Effacer les filtres
Effacer les filtres

comparision of data with logic given as input

1 vue (au cours des 30 derniers jours)
Sumanth
Sumanth le 27 Fév 2023
This is a sample data for 3. I am stuck at how to write the same logic for 10 such that there should be comparision for all the input values given for T1 ,T2, .... T10.
I = zeros(1,3);
if T1 == T2
I(1,1)= 1;
I(1,1)= 0;
elseif T1 > T2
I(1,1)= 1;
I(1,2)= 0;
elseif T1 < T2
I(1,1)= 0;
I(1,2)= 1;
end
note: only one element in I should be equal to 1 in the end in such a way that the highest T gets the value 1 in I.

Réponse acceptée

Voss
Voss le 27 Fév 2023
Modifié(e) : Voss le 27 Fév 2023
Assuming T1, ..., T10 are scalars,
T = [T1 T2 T3 T4 T5 T6 T7 T8 T9 T10];
[~,idx] = max(T);
I = zeros(size(T));
I(idx) = 1;
  2 commentaires
Sumanth
Sumanth le 28 Fév 2023
Thank you so much! This works!
Voss
Voss le 28 Fév 2023
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 27 Fév 2023
Can you dynamically create and operate on variables with numbered names like T1, T2, T3, etc.? Yes.
Should you do this? The general consensus is no. That Answers post explains why this is generally discouraged and offers several alternative approaches.

Community Treasure Hunt

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

Start Hunting!

Translated by