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.

 Accepted Answer

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 Comment

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

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!