Error: Matrix dimensions must agree

1 view (last 30 days)
bl276
bl276 on 20 Apr 2021
Commented: bl276 on 20 Apr 2021
I am trying to execute this piece of code but cannot figure out why I am getting this error, any help is much appriciated.
yp=kron(eye(32),[-1/.5 0;1 0]).*ones(2*32,1)+80*pi.*kron(ones(32),1);[1/.5;0]+2.*90.*diag(ones(2*32-1,1),1).*(kron(ones(32,1),[;1/.5])).*cos(zeros(2*32-1,1)).*(sin(8.*((1/64):(1/64):10)-ones(2*32,1)))./.5

Accepted Answer

Jan
Jan on 20 Apr 2021
Edited: Jan on 20 Apr 2021
Seriously? Your code contains some jokes:
  • -1/.5 == 2
  • kron(eye(32), [-2, 0; 1, 0]) .* ones(64, 1) == kron(eye(32), [-2, 0; 1, 0])
  • kron(ones(32), 1) == ones(32)
So a simplified version of the first line is:
yp = kron(eye(32), [-2, 0; 1, 0]) + 80 * pi * ones(32);
% [64 x 64] + [32 x 32]
This cannot work. If you really want to add this, omit the expanding of the 2nd term to a matrix:
yp = kron(eye(32), [-2, 0; 1, 0]) + 80 * pi;
The 2nd part ise useless at all, even if it would work, because the result is not assigned to a variable. So the best solution is simply to delete the 2nd line. By the way, this line contains invalid Matlab syntax:
[1/.5 ; 0] + 2 .* 90 .* diag(ones(63, 1), 1) .* ...
(kron(ones(32, 1), [;1/.5])) .* ...
... % ^ what should this be?!?
cos(zeros(63, 1)) .* sin(8 .* ((1/64):(1/64):10) - ones(64, 1)) ./ .5
% [63 x 1] .* [64 x 640] : cannot match
Expressions like cos(zeros(2*32-1,1)) are a waste of time, because cos(0)==1, so this replace this by ones(63,1).
But as said already: because this part of the code does not perform anything useless, it is easier to delete it than to fix it.
I cannot image a serious task, in which such code could appear.
  1 Comment
bl276
bl276 on 20 Apr 2021
Thanks Jan,
The code comes from a published book. I also could not see how this code could work as is. I did make a typo when eliminating the variables ( [0:1/.5] is what it should read). The code is intened to model pattern formation in the hippocampus.
Here is a snapshot of the code from the book if you are intrigued:
(The part I attempted to shows is at the bottom, this is where I got the matrix error)

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!