Writing a for loop for matrices in MATLAB

152 Views Asked by At

I am a complete Newbie at MATLAB, and am very confused with the manipulation of matrices using for-loops.

suppose we are given a 6x5 matrix(A), the goal is to write a for loop in MATLAB that can isolate each of the columns of the matrix and then stores that column into a new and empty array(x = []) that I can use later on.

Could someone please help!?

Thank you!

1

There are 1 best solutions below

0
On BEST ANSWER

To call one column of a matrix, use A(:,j) where j is the column number you need (thankfully MATLAB is one-based, meaning arrays begin indexing at the number one). Similarly to call rows use A(i,:).

You really can't loop this well, as far as I know. But, if your loop does do something with the columns independently, then perhaps you need something like this:

for j = 1:dim(A, 2) %dim(A, 2) returns the number of columns
    xcol = A(:, j);
    statements
end