# Matlab, loop for. Problem.

1 view (last 30 days)
Przemek Tomaszewski on 30 Aug 2018
Hi.
How to simplify the bolded code. I would like to put it in a loop. But it does not really work out. Maybe someone will tell you.
In the attachment I added the data (Ma and Nf) need for the program.
for i=1:10
Moment_sredni(:,i)=mean(Ma((10:130),i));
Moment_NF(:,i)=Moment_sredni(i)/1.15;
ind(:,i)=Ma(:,i)<Moment_NF(:,i);
end
I would like the code below to simplify and put in a loop
Nf1=Nf(:,1); trwalosc1=Nf1(ind(:,1)); TRWALOSC1=trwalosc1(1);
Nf2=Nf(:,2); trwalosc2=Nf2(ind(:,2)); TRWALOSC2=trwalosc2(1);
Nf3=Nf(:,3); trwalosc3=Nf3(ind(:,3)); TRWALOSC3=trwalosc3(1);
Nf4=Nf(:,4); trwalosc4=Nf4(ind(:,4)); TRWALOSC4=trwalosc4(1);
Nf5=Nf(:,5); trwalosc5=Nf5(ind(:,5)); TRWALOSC5=trwalosc5(1);
Nf6=Nf(:,6); trwalosc6=Nf6(ind(:,6)); TRWALOSC6=trwalosc6(1);
Nf7=Nf(:,7); trwalosc7=Nf7(ind(:,7)); TRWALOSC7=trwalosc7(1);
Nf8=Nf(:,8); trwalosc8=Nf8(ind(:,8)); TRWALOSC8=trwalosc8(1);
Nf9=Nf(:,9); trwalosc9=Nf9(ind(:,9)); TRWALOSC9=trwalosc9(1);
Nf10=Nf(:,1); trwalosc10=Nf10(ind(:,10)); TRWALOSC10=trwalosc10(1);
Anusha Sridharan on 9 Jan 2019

OCDER on 30 Aug 2018
DO NOT label your variables Nf1, Nf2, .... Var1, Var2, ...
Use cell arrays like this:
NfC = cell(1, size(Nf,2));
trwalosc = cell(size(NfC));
TRWALOSC = cell(size(NfC)); %You sure you want to label 2 variable with similar names, different by caps?
for j = 1:size(Nf, 2)
NfC{j} = Nf(:,j);
trwalosc{j} = NfC{j}(ind(:, j));
TRWALOSC{j} = trwalosc{j}(1);
end
Przemek Tomaszewski on 30 Aug 2018
Thank you very much for your help :) Everything works as it should.