0

I need to make a for loop in MATLAB to divide each column in a matrix by a separate column vector. I only want to do this for a selection of the columns in the matrix, not all the columns.

This is what I'd like to do, where Indexes is a 19x1 vector of integers (not all consecutive numbers), big_matrix is 82x24, and other_column is 82x1:

matrix_to_fill = zeros(82,length(Indexes));
for x = Indexes
    new_column = big_matrix(:,x)./other_column;
    new_index = find(Indexes==x);
    matrix_to_fill(:,new_index) = new_column;
end

When I run this I get the following error:

Error using  ./ 
Matrix dimensions must agree.

I can run each iteration separately without getting errors, so I know that the matrix dimensions agree. What's more, if I type out the Indexes as a vector it works fine:

matrix_to_fill = zeros(82,length(Indexes));
for x = [1,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,23]
    new_column = big_matrix(:,x)./other_column;
    new_index = find(Indexes==x);
    matrix_to_fill(:,new_index) = new_column;
end

And I think the "x=Indexes" syntax is fine because I've tested that using just:

for x = Indexes
    disp(x)
end

So I'm completely stumped. Any help would be much appreciated!

  • 1
    Use [MATLAB's debugger](http://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html), or [`dbstop if error`](http://www.mathworks.com/help/matlab/ref/dbstop.html) more specifically, to evaluate the workspace when your error is thrown. – sco1 Sep 07 '15 at 23:30
  • 1
    What if you replace `other_column` in the loop with `repmat(other_column,1,numel(Indexes))`? This is likely because in the 1st case you use `for x = Indexes` and Matlab performs the operation on all the columns at once, whereas if you use `for x = [1 4 5 ...] ` then it loops through each element of `Indexes` individually so the dimensions agree. – Benoit_11 Sep 07 '15 at 23:34
  • Be wary of how `for` loops operate in MATLAB. `for` loops can accept a **matrix** as input and each iteration of the loop index takes in one column of the matrix at a time. See the duplicate linked post for more details. `Indexes` is a column vector in your case, which is why MATLAB only iterates this loop once. Phil's answer to transpose `Indexes` prior to looping solves your problem. – rayryeng Sep 08 '15 at 00:10

1 Answers1

2

The problem is in your definition of the for loop. When you say that you think the "x=Indexes" syntax is correct you haven't been observant enough to see that it is not correct.

What you need is

for x = Indexes'
% Do your looping
end

Note the transpose in the above.

If you do

for x = Indexes
    disp(x)
end

Then the loop is executed once, with x taking on the value of the whole vector.

If you do

for x = Indexes'
    disp(x)
end

then x will take on the individual elements of the matrix and you'll have 19 scalars displayed, once each time through the loop.

Phil Goddard
  • 10,571
  • 1
  • 16
  • 28
  • Though this is technically correct and you've noticed why the loop isn't working, it may be fruitful to explain **why** this happens. MATLAB's `for` loop syntax can accept matrices as input and the loop iteration variable is set to columns of a matrix at each iteration. This post by chappjc may provide some insight: http://stackoverflow.com/questions/23299617/multiple-loop-variables-matlab – rayryeng Sep 08 '15 at 00:12
  • My answer is more than "technically" correct. It is absolutely correct. Your comment provides more info, and yes that is the reason why, but is not required to answer the question. – Phil Goddard Sep 08 '15 at 01:54