MATLAB: Hello ! I am new to MATLAB, and i need a small help in using it ..

for loopiterationloop

I am getting a value between 1 and 16 from previous section of the program and storing it as 'I1'. s1, s2 … s16 are matrices of column = 1 and rows = 65536 under MATLAB workspace. Based on the value of 'I1' , the matrix is selected. For instance, if 'I1' is 1 then s1 is selected. Then reshape command is used to split the matrix into 8 columns. Then RMS of each column is found and stored as 'y'. Then the maximum value of 'y' is displayed. Currently I am using the following code..
for ii = 1:16
if (I1==1)
reshape (s1,8192,[]);
y = sqrt(sum(ans.*conj(ans))/size(ans,1))
[M2,I2]=max(y)
elseif (I1==2)
reshape (s2,8192,[]);
y = sqrt(sum(ans.*conj(ans))/size(ans,1))
[M2,I2]=max(y)
.
.
.
elseif (I1==16)
reshape (s16,8192,[]);
y = sqrt(sum(ans.*conj(ans))/size(ans,1))
[M2,I2]=max(y)
end
end
Is there any other method which uses iterations , loops or anything else to shorten its length?
Thanks in advance !!

Best Answer

First, put in a line
s = [s1, s2 ... s16];
Then, replace the entire if statement with
reshape (s(:,I1),8192,[]);
y = sqrt(sum(ans.*conj(ans))/size(ans,1))
[M2,I2]=max(y)
Note that, in general, it is a bad coding practice to have variables s1, s2, s3, etc. Those all could have been coded to a single matrix s in the first place.