Subscripted assignment dimension mismatch

I am receiving this error in my code and I cannot figure out why. Any suggestions would be greatly appreciated.
[M,N,K] = size(X);
for j=1:K,
mu(j) = mean(X(:,:,j));
X(:,:,j) = X(:,:,j) - mu(j);
end
This goal is simply to take the mean of the third component and subtract it out from itself. There's a lot more going on in the code but this point is giving me the trouble.

 采纳的回答

Benjamin - put a breakpoint at the line
mu(j) = mean(X(:,:,j));
and run your code. When the debugger pauses at this line, check to see what is
mean(X(:,:,j))
Is it a scalar or a 1xN array which has the mean of each column? What are you expecting it to be?
I suspect, given your statement, that you are assuming that it is a scalar that you can then subtract out from every other element, but that isn't the case. If this is true, then what you can do is either
mu(j) = mean(mean(X(:,:,j)));
or
T = X(:,:,j);
mu(j) = mean(T(:));
See mean for details on this function.

1 个评论

I see what you're saying and looking at this with a fresh mind, the problem makes sense to me now. Thanks!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by