comparision of data with logic given as input

1 view (last 30 days)
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.

Accepted Answer

Voss
Voss on 27 Feb 2023
Edited: Voss on 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;

More Answers (1)

Steven Lord
Steven Lord on 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!