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

조회 수: 3 (최근 30일)
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?

답변 (1개)

Stephen23
Stephen23 2017년 10월 23일
편집: Stephen23 2017년 10월 23일
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
Stephen23
Stephen23 2017년 10월 23일
편집: Stephen23 2017년 10월 23일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by