Error: Matrix dimensions must agree

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

 采纳的回答

Jan
Jan 2021-4-20
编辑:Jan 2021-4-20
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 个评论

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)

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品

版本

R2020b

标签

Community Treasure Hunt

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

Start Hunting!

Translated by