Matlab, loop for. Problem.

1 view (last 30 days)
Przemek Tomaszewski
Przemek Tomaszewski on 30 Aug 2018
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
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);
  1 Comment
Anusha Sridharan
Anusha Sridharan on 9 Jan 2019
[Answers Dev] Restored edits

Sign in to comment.

Accepted Answer

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);
  1 Comment
Przemek Tomaszewski
Przemek Tomaszewski on 30 Aug 2018
Thank you very much for your help :) Everything works as it should.

Sign in to comment.

More Answers (0)


Find more on Loops and Conditional Statements 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!