Dear MatLab comunity,
I have a problem with a for loop.
I have two arrays of angles Theta1 and Theta2 (attached).
I have defined the following:
rTheta1 = load('theta1.dat');
rTheta2 = load('theta2.dat');
Theta1 = Theta1(:,2); %% which are the Theta1 angles
Theta2 = Theta2(:,2); %% // Theta2 angles
Theta1_cis = Theta1((Theta1 > -90) & (Theta1 < 90)); % Array of values when theta1 is in Cis
Theta1_trans = Theta1(Theta1 > 90 | Theta1 < -90); % Array of values when theta1 is in Trans
What I need now is to define an array such that when Theta1 is whitin the cis range then I collect the value in the variable Theta2_T1cis otherwise it goes in Theta2_T1trans, or when Theta1 is in the trans range then it goes in Theta2_T1trans.
I tried to put up a loop but I am very nabish so please help me on this:
i = 1;
for i = 1:[Theta1]
if Theta1((Theta1 > -90) & (Theta1 < 90))
Theta2_T1cis(i) = Theta2(i)
else Theta2_trans(i) = Theta2(i)
i= i+1;
end
end
this is not working of course. How can I solve it?
Hope the question is clear, otherwise I will reformulate.
Thanks in advance,
Alessandro

 Respuesta aceptada

Jan
Jan el 21 de Feb. de 2022
Editada: Jan el 21 de Feb. de 2022

0 votos

There is no need for a loop:
T1_cis_index = (Theta1 > -90) & (Theta1 < 90);
T1_trans_index = (Theta1 > 90) | (Theta1 < -90);
Theta1_cis = Theta1(T1_cis_index);
Theta1_trans = Theta1(T1_trans_index);
Theta2_T1cis = Theta2(T1_cis_index);
Theta2_T1trans = Theta2(T1_trans_index);
By the way, this is no the way loops are working. A cleaner version:
% i = 1; Nope
for i = 1:numel(Theta1) % Not: [Theta1]
if (Theta1(i) > -90) & (Theta1(i) < 90)) % Use Theta1(i), not the complete vector
Theta2_T1cis(i) = Theta2(i)
else
Theta2_trans(i) = Theta2(i)
% Nope: i= i+1;
end
end

3 comentarios

Alessandro Ruda
Alessandro Ruda el 21 de Feb. de 2022
Thanks for the answer,
I tried them but they don't work. The loops gives me two arrays:
  • Theta2_T1cis ordered in columns and not rows with zeroes and Theta2 values depending on when the if conditions is satisfied, but I don't want the zeroes.
  • Theta2_T1trans that is the basically the same exact array as Theta2.
The first method doesn't seem to work either in my hands.
Alessandro Ruda
Alessandro Ruda el 21 de Feb. de 2022
Editada: Alessandro Ruda el 21 de Feb. de 2022
Ok, it actually works. But I had to change it this way to obtain what I wanted specifically.
for i = 1:numel(Theta1)
if ((Theta1(i) > -90) & (Theta1(i) < 90))
Theta2_T1cis_raw(i) = Theta2(i);
else
Theta2_T1trans_raw(i) = Theta2(i);
end
end
tTheta2_T1cis = Theta2_T1cis_raw(Theta2_T1cis_raw~=0);
Theta2_T1cis = transpose(tTheta2_T1cis);
tTheta2_T1trans = Theta2_T1trans_raw(Theta2_T1trans_raw~=0);
Theta2_T1trans = transpose(tTheta2_T1trans);
I don't know if it can be made cleaner!
Thank you anyway!
Jan
Jan el 22 de Feb. de 2022
Editada: Jan el 22 de Feb. de 2022
The loop is not needed. A shorter, leaner and faster solution:
match = (Theta1 > -90) & (Theta1 < 90);
Theta2_T1cis = Theta2(match);
tTheta2_T1trans = Theta2(~match);

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2020b

Etiquetas

Preguntada:

el 21 de Feb. de 2022

Editada:

Jan
el 22 de Feb. de 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by