MATLAB: How to reference the element of a matrix in a loop

MATLABmatrix manipulation

I have a matrix 6×37 stability_cond with 1 and twos
[a,b]=find(stability_cond==1);
idx1=[a,b];
[c,d]=find(stability_cond==2);
idx2=[c,d];
The code to find the position of 1 and 2 in stability_cond.
I want to use the position to fix stability_class using conditions in the variable WindSp using the code below
for s=1:138
for k=idx1(1:end,:)
for l=idx1(:,1:end)
if (WindSp(k(s,1),l(s,1))<2)
stability_class(k(s,1),l(s,1))=1;
elseif(WindSp(k(s,1),l(s,1)==2)|WindSp(k(s,1),l(s,1))<3)
stability_class(k(s,1),l(s,1))=2;
elseif(WindSp(k(s,1),l(s,1))==3|WindSp(k(s),l(s))<5) %#ok<*OR2>
stability_class(k(s,1),l(s,1))=3;
elseif((WindSp(k(s,1),l(s,1))==5)|(WindSp(k(s,1),l(s,1))<6))
stability_class(k(s,1),l(s,1))=4;
else
stability_class(k(s,1),l(s,1))=5;
end
end
end
end
I keep getting the error: Index exceeds matrix dimensions.

Best Answer

k and l inside the for-loops are scalars, thus you can't take an indexing k(s) or l(s).
Now what your code supposes to do and how to fix it, nobody would know.
What I *guess* is
k=idx1(:,1);
l=idx1(1,:)';
for s=1:138
if (WindSp(k(s,1),l(s,1))<2)
stability_class(k(s,1),l(s,1))=1;
elseif(WindSp(k(s,1),l(s,1)==2)|WindSp(k(s,1),l(s,1))<3)
stability_class(k(s,1),l(s,1))=2;
elseif(WindSp(k(s,1),l(s,1))==3|WindSp(k(s),l(s))<5) %#ok<*OR2>
stability_class(k(s,1),l(s,1))=3;
elseif((WindSp(k(s,1),l(s,1))==5)|(WindSp(k(s,1),l(s,1))<6))
stability_class(k(s,1),l(s,1))=4;
else
stability_class(k(s,1),l(s,1))=5;
end
end