Reputation: 212
I'm very new to MATLAB and not sure about this. I've a matrix
matrix = [1 2 3 4;8 9 5 6];
Now I want to iterate through the columns of the above matrix and retrieve the column previous to the current one. So if while iterating, we're at column 2 then we should retrieve column 1.
Can someone please point me in the right direction? I tried
for v = matrix
disp(v-1)
end
but that didn't work. Any help will be greatly appreciated.
Upvotes: 2
Views: 1769
Reputation: 756
First, we need to find how many columns there are in your matrix:
m = [1,2,3,4;9,8,5,6]
[rows, cols] = size(m)
Next, we'll cycle through all the columns and print out current column:
for ii=1:1:cols
disp('current column: ')
m(:,ii) % the : selects all rows, and ii selects which column
end
If you wanted the previous column, rather than the current column:
for ii=1:1:cols
if ii == 1
disp('first column has no previous!')
else
disp('previous column: ')
m(:,ii-1) % the : selects all rows, and ii selects columns
end
end
Upvotes: 4