I am trying to divide and getting the error message "matrix dimensions must agree". What does this mean?

3 views (last 30 days)
I have the following piece of code.
for ii = 1:length(k);
for jj = length(trials);
V1 = trials(jj,1) / (1 + ([k,ii] * 0));
V2 = trials(jj,2) / (1 + ([k,ii] * trials(jj,3)));
P2(jj,ii) = 1 / (1 + exp(-B*(V2-V1)));
end
end
When I run the code, I get the following error message.
Error using /
Matrix dimensions must agree.
Error in OptimalStimuliChoiceFINAL (line 31)
V1 = allOptions(jj,1) / (1 + ([k,ii] * 0));
It should be an equation, with figure before the line divisible by the product of the sum after the line, so I am unsure why this error is occurring.
How do I go about fixing this error?

Answers (1)

Stephen23
Stephen23 on 23 Oct 2017
Edited: Stephen23 on 23 Oct 2017
You need to use element-wise rdivide ./, not matrix mrdivide /. I.e. you need this:
V1 = trials(jj,1) ./ (1 + ([k,ii] * 0));
^ note the dot: mrdivide!
for all of the division operations. It is very important to know the differences between these kind of operations. You can read about them by reading the MATLAB documentation:
  1 Comment
Stephen23
Stephen23 on 23 Oct 2017
Edited: Stephen23 on 23 Oct 2017
Depending on what size B is, your entire RHS is likely non-scalar (i.e. a vector of values). But the indices on the LHS only specify one position (i.e. one value or number). Simply put, you cannot put multiple numbers into the place of one number. That is not how matrices work!
You have not written any code comments, and there is no explanation of what the algorithm should be doing, so I have no idea what you are trying to achieve with that code.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!