How to solve this index exceeds matrix dimensions error?
Show older comments
Hello this is my code:
N = 30;
Sw = 0;
ni=8; %numero campioni
for i = 1:N
for j = 1:ni
x_diff = X(i,j) - mean(X(i,2));
Sw = Sw + (x_diff * x_diff');
end
end
Sb = 0;
for i = 1:N
x_diff = mean(X(i,N)) - mean(X);
Sb = Sb + (N(i) * x_diff * x_diff');
end
how can i solve the problem of the index exceeds matrix dimensions?
5 Comments
Birdman
on 21 Nov 2017
What is X?
Jessica Frau
on 21 Nov 2017
If X has one column as you say, then why you try to reach to 8th column in first nested for loop and 2nd column in mean operation?
for i = 1:N
for j = 1:ni
x_diff = X(i,j) - mean(X(i,2));
Sw = Sw + (x_diff * x_diff');
end
end
Jessica Frau
on 21 Nov 2017
Edited: Jessica Frau
on 21 Nov 2017
Guillaume
on 21 Nov 2017
I don't understand your equations, particularly why there is a transpose symbol on something that look like scalar values, but in any case, it is clear that X is supposed to be at least a 2D matrix.
If you don't have a 2D matrix I'm not sure how we can help.
Answers (1)
Guillaume
on 21 Nov 2017
As you've stated X is a Nx1 vector. Yet, you have:
X(i, j)
X(i, 2)
X(i, N)
in your code. How are you expecting that to work?
Categories
Find more on Matrix Indexing 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!