MATLAB: Attempted to access x(12); index out of bounds because numel(x)=11.

index out of bounds

What's wrong with this line A(n,1)= x(n); ?? I find it perfectly correct.
x = load('S_chest');
y = load('S_abdomen');
for i= 1:length(y)
w= [y(i)/(x(i)+x(i-1))];
y(i)= x(i)*w*1+x(i-1)*w*2;
end

Best Answer

For your first piece of code, nothing guarantees that x is the same length as y.
For your second piece of code, you assign the char array 'S_chest.mat' of length 11 to x (so x(1) is simply 'S', x(2) simply '_', etc.), so of cource accessing x(12) is an error. Not sure what you meant to do.