Filter löschen
Filter löschen

comparision of data with logic given as input

1 Ansicht (letzte 30 Tage)
Sumanth
Sumanth am 27 Feb. 2023
Bearbeitet: Walter Roberson am 28 Feb. 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.

Akzeptierte Antwort

Voss
Voss am 27 Feb. 2023
Bearbeitet: Voss am 27 Feb. 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 Kommentare
Sumanth
Sumanth am 28 Feb. 2023
Thank you so much! This works!
Voss
Voss am 28 Feb. 2023
You're welcome!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Steven Lord
Steven Lord am 27 Feb. 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