Error using / matrix dimensions must agree

1 view (last 30 days)
risky amalia
risky amalia on 10 Jun 2021
Answered: Walter Roberson on 10 Jun 2021
for i=1:M
Prob(i) = Datfit(i)/sumfitness;
end
halo. here's my code. i want to know the solution bcs there's always error
  1 Comment
Sebastiano Marinelli
Sebastiano Marinelli on 10 Jun 2021
May ask you more information:
  • What are you tring to do?
  • What is the value of M?
  • What is Datfit and sumfitness?
  • Can you post the error?
anyway if the problem is the one on the titile it may due to an error on the division, maybe sumfitness has a different dimension than the Datafit, but we need more info :)

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 10 Jun 2021
We can tell from the error that sumfitness is not a scalar, and so the / operator is the matrix right division operator, which would be similar in effect to Datfit(i) * pinv(sumfitness) if the operation were permitted.
The / operator requests to solve simultaneous linear equations for best fit, but that requires compatible sizes on the left and right size of the equations.
We might suspect that you want element-by-element division, which would be Datfit(i) ./ sumfitness . However we can tell from the error that if you were to change that then the result you got from the right hand side would be a vector and the vector would not fit into the output location.
So... either you have to fix sumfitness to be a scalar instead of a vector, or else we have to guess that you might mean something like
Prob(i) = Datfit(i) ./ sumfitness(i);

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!