Read about logical indexing: Matrix = [20,5; 30, -6; 40,8; 50,10]; for i = 1:size(Matrix,1) if Matrix(i,1)<0 || Matrix(i,2)<0 disp('neg') elseif Matrix(i,1)>0 && Matrix(i,2)>0 % some function end end Answer from madhan ravi on mathworks.com
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 297463-how-to-create-an-mxn-matrix-with-a-for-loop
How to create an mxn matrix with a for-loop - MATLAB Answers - MATLAB Central
July 28, 2016 - MATLAB is a high-level language, so you don't need to rely on loops to solve all of your tasks (see dpb's comment how). There is also no point in reinventing the wheel: you can use the excellent FEX submission permn ... https://www.mathworks.com/matlabcentral/answers/297463-how-to-create-an-mxn-matrix-with-a-for-loop#comment_381993
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 45299-how-to-create-a-loop-for-matrix-iteration
how to create a loop for matrix iteration - MATLAB Answers - MATLAB Central
August 5, 2012 - hello everybody, I'm beginner of matlab. I would like obtain z1, z2, z3 and z4 with a for loop, but I couldn't write it. n=8; p=5; A=[1:n]; for k=2:p for t=1:n ...
Top answer
1 of 8
95

You can use linear indexing to access each element.

for idx = 1:numel(array)
    element = array(idx)
    ....
end

This is useful if you don't need to know what i,j,k, you are at. However, if you don't need to know what index you are at, you are probably better off using arrayfun()

2 of 8
36

The idea of a linear index for arrays in matlab is an important one. An array in MATLAB is really just a vector of elements, strung out in memory. MATLAB allows you to use either a row and column index, or a single linear index. For example,

A = magic(3)
A =
     8     1     6
     3     5     7
     4     9     2

A(2,3)
ans =
     7

A(8)
ans =
     7

We can see the order the elements are stored in memory by unrolling the array into a vector.

A(:)
ans =
     8
     3
     4
     1
     5
     9
     6
     7
     2

As you can see, the 8th element is the number 7. In fact, the function find returns its results as a linear index.

find(A>6)
ans =
     1
     6
     8

The result is, we can access each element in turn of a general n-d array using a single loop. For example, if we wanted to square the elements of A (yes, I know there are better ways to do this), one might do this:

B = zeros(size(A));
for i = 1:numel(A)
  B(i) = A(i).^2;
end

B
B =
    64     1    36
     9    25    49
    16    81     4

There are many circumstances where the linear index is more useful. Conversion between the linear index and two (or higher) dimensional subscripts is accomplished with the sub2ind and ind2sub functions.

The linear index applies in general to any array in matlab. So you can use it on structures, cell arrays, etc. The only problem with the linear index is when they get too large. MATLAB uses a 32 bit integer to store these indexes. So if your array has more then a total of 2^32 elements in it, the linear index will fail. It is really only an issue if you use sparse matrices often, when occasionally this will cause a problem. (Though I don't use a 64 bit MATLAB release, I believe that problem has been resolved for those lucky individuals who do.)

๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 1729880-using-for-loop-to-extract-data-from-every-column-in-a-matrix
Using for-loop to extract data from every column in a matrix - MATLAB Answers - MATLAB Central
May 30, 2022 - So you have a matrix, the basic data format for easily storing and efficiently processing data within MATLAB (in fact even the name comes from "MATrix LABoratory", giving a big hint that using matrices is probably a very good idea). "I want to be able to write a for loop that for each column, a new variable is created that includes the data from the rows 3000-13600."
Find elsewhere
๐ŸŒ
MathWorks
mathworks.com โ€บ matlab โ€บ language fundamentals โ€บ loops and conditional statements
for - for loop to repeat specified number of times - MATLAB
Suppose that the loop end value is equal to or close to the maximum or minimum value for the loop index data type. In the generated code, the last increment or decrement of the loop index might cause the index variable to overflow. The index overflow might result in an infinite loop. See Loop Index Overflow (MATLAB Coder).
๐ŸŒ
MathWorks
mathworks.com โ€บ videos โ€บ making-a-matrix-in-a-loop-in-matlab-97238.html
How to Make a Matrix in a Loop in MATLAB - MATLAB
And we want to produce the following matrix. How do we go about doing this? So the first step is to figure out the pattern. In this case, each column's values are double the values in the column before it. So just like before, let's create our initial vector, B. And this time, we're appending three more columns to B. So when we set up our loop, we'll say, for I equals 2:4. Next up is writing the pattern in MATLAB ...
Published ย  August 18, 2017
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 1564501-extracting-every-row-from-a-matrix-using-a-for-loop
Extracting every row from a matrix using a for loop - MATLAB Answers - MATLAB Central
October 15, 2021 - Are you aware of a format whereby I can write a loop that takes the data from every row of the 23,999 matrix? So starting with row 1 and extracting all the data in that row and then repeating the process for every row in the matrix, for all 23,999 rows? ... https://www.mathworks.com/matlabcentral/answers/1564501-extracting-every-row-from-a-matrix-using-a-for-loop#comment_1786121
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 480712-how-to-iterate-through-a-matrix-using-for-loops
How to iterate through a Matrix using for loops? - MATLAB Answers - MATLAB Central
September 17, 2019 - For each value of Ke, you get one KG matrix. So the first loop is indeed running all values of theta but the second loop is overwriting KG with each value of Ke. @walter sir: You missed to give any suggestion that'll help solving the issue. My recommendation is to store all the KG matrices in a cell array. Please suggest any better alternative if you have any in mind. Thanks again :) ... https://www.mathworks.com/matlabcentral/answers/480712-how-to-iterate-through-a-matrix-using-for-loops#comment_746697
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 1626400-how-do-i-loop-a-fucntion-over-matrix-columns-and-store-the-results-in-a-column
How do I loop a fucntion over matrix columns and store the results in a column? - MATLAB Answers - MATLAB Central
January 10, 2022 - https://www.mathworks.com/matlabcentral/answers/1626400-how-do-i-loop-a-fucntion-over-matrix-columns-and-store-the-results-in-a-column#comment_1930945 ... This works! Very cool and thank you for the support.
๐ŸŒ
MathWorks
mathworks.com โ€บ matlabcentral โ€บ answers โ€บ 669803-how-can-i-put-the-outputs-of-for-loop-into-a-matrix
How can I put the outputs of for loop into a matrix? - MATLAB Answers - MATLAB Central
November 30, 2020 - Hey i am new to matlab and need help with storing answer of my for loop into a matrix can anyone help ? my aim is having (n x n) matrix. thanks a lot n=input('please enter n>'); for j=1:n ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ software engineering โ€บ how-to-iterate-through-each-element-in-n-dimensional-matrix-in-matlab
How to Iterate through each element in N-Dimensional matrix in MATLAB? - GeeksforGeeks
October 20, 2021 - for i=1:n output(i)=M(i); end % prints output array. output ... Here the output is different from above because elements of a matrix are accessed column-wise. As the loop goes from 1 to a number of elements in the matrix, every element is accessed ...