how can i call vector and their element at a time for comparision
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Chaudhary P Patel
le 30 Avr 2022
Commenté : Walter Roberson
le 30 Avr 2022
Respected Sir/Madam
i have
Fr1= [0.8147; 0.9058; 0.1270; 0.9134; 0.6324]
Fr2= [0.0975; 0.2785; 0.5469; 0.9575; 0.9649]
Fr3=[0.1576; 0.9706; 0.9572; 0.4854; 0.8003]
Fr4=[0.1419; 0.4218; 0.9157; 0.7922; 0.9595]
Fr5=[0.6557; 0.0357; 0.8491; 0.9340; 0.6787]
Fs=[0.7577; 0.7431; 0.3922; 0.6555; 0.1712]
Now i want to compare each Fr (variable vector) element individually with Fs element .
from the individual comparison how can i write that any (at least one) of the Fr element is less than or equal to the corresponding element of Fs.
2 commentaires
Walter Roberson
le 30 Avr 2022
Do you mean that for any given variables such as Fr1, you want to test that there is at least one element that is less than the corresponding Fs element? Or do you want to test that there is at least one element in Fr1 that is less than some element somewhere in Fs ?
Réponse acceptée
Alberto Cuadra Lara
le 30 Avr 2022
Hi Chaudhary,
A loop using the function any will be my approach.
% Input
Fr1 = [0.8147; 0.9058; 0.1270; 0.9134; 0.6324];
Fr2 = [0.0975; 0.2785; 0.5469; 0.9575; 0.9649];
Fr3 = [0.1576; 0.9706; 0.9572; 0.4854; 0.8003];
Fr4 = [0.1419; 0.4218; 0.9157; 0.7922; 0.9595];
Fr5 = [0.6557; 0.0357; 0.8491; 0.9340; 0.6787];
Fs = [0.7577; 0.7431; 0.3922; 0.6555; 0.1712];
% Create matrix
FR = [Fr1, Fr2, Fr3, Fr4, Fr5];
% Check for values less than any of the values in Fs
for i = length(FR):-1:1
FLAG(i) = any(FR(:, i) < Fs);
% Print result
if FLAG(i)
fprintf('Satisfied condition in Fr%d\n', i)
else
fprintf('Unsatisfied condition in Fr%d\n', i)
end
end
13 commentaires
Walter Roberson
le 30 Avr 2022
Because computers only do what they are told.
Imagine if I had coded
for j = 1 : 6
if f_s(j,i+1)<=f_r(j,1)
found_one = true;
break;
end
end
Now after that loop has finished, what is the value of the variable found_one ?
If one of the six f_s entries matched, then found_one was assigned true and the loop stopped.
But suppose none of the six f_s entries matched: then what is the value of found_one in that case?
The answer is: found_one would be undefined in that case. Because computers are too stupid to automatically know that if you did not assign "true" to something, that it should be "false". (More generally, there are a number of circumstances under which it can be important to know the difference between "found", "definitely not found", and "no information known")
Plus de réponses (2)
Ahmad
le 30 Avr 2022
Modifié(e) : Ahmad
le 30 Avr 2022
Fr1=[0.8147; 0.9058; 0.1270; 0.9134; 0.6324]
Fr2=[0.0975; 0.2785; 0.5469; 0.9575; 0.9649]
Fr3=[0.1576; 0.9706; 0.9572; 0.4854; 0.8003]
Fr4=[0.1419; 0.4218; 0.9157; 0.7922; 0.9595]
Fr5=[0.6557; 0.0357; 0.8491; 0.9340; 0.6787]
Fs =[0.7577; 0.7431; 0.3922; 0.6555; 0.1712]
Fr=[Fr1 Fr2 Fr3 Fr4 Fr5]
Frmin=min(Fr)<=max(Fs) % ==> [1 1 1 1 1] means All Frn pass the condition
Alberto Cuadra Lara
le 30 Avr 2022
Modifié(e) : Alberto Cuadra Lara
le 30 Avr 2022
Hello Chaudhary,
I see... Okay so you need to use the eval function, but it will be much slower.
% Input
Fr1 = [0.8147; 0.9058; 0.1270; 0.9134; 0.6324];
Fr2 = [0.0975; 0.2785; 0.5469; 0.9575; 0.9649];
Fr3 = [0.1576; 0.9706; 0.9572; 0.4854; 0.8003];
Fr4 = [0.1419; 0.4218; 0.9157; 0.7922; 0.9595];
Fr5 = [0.6557; 0.0357; 0.8491; 0.9340; 0.6787];
Fs = [0.7577; 0.7431; 0.3922; 0.6555; 0.1712];
% Definitions
N = 5; % Number of cases
tic
% Check for values less than any of the values in Fs
for i = N:-1:1
Fr = eval(strcat('Fr', sprintf('%d', i)));
FLAG(i) = any(Fr < Fs);
% Print result
if FLAG(i)
fprintf('Satisfied condition in Fr%d\n', i)
else
fprintf('Unsatisfied condition in Fr%d\n', i)
end
end
fprintf('Execution time %f\n', toc)
tic
% Create matrix
FR = [Fr1, Fr2, Fr3, Fr4, Fr5];
% Check for values less than any of the values in Fs
for i = length(FR):-1:1
FLAG(i) = any(FR(:, i) < Fs);
% Print result
if FLAG(i)
fprintf('Satisfied condition in Fr%d\n', i)
else
fprintf('Unsatisfied condition in Fr%d\n', i)
end
end
fprintf('Execution time %f\n', toc)
0 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!