how can i call vector and their element at a time for comparision

1 visualización (últimos 30 días)
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 comentarios
Walter Roberson
Walter Roberson el 30 de Abr. de 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 ?
Chaudhary P Patel
Chaudhary P Patel el 30 de Abr. de 2022
@Walter Roberson sir, i want it, 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.

Iniciar sesión para comentar.

Respuesta aceptada

Alberto Cuadra Lara
Alberto Cuadra Lara el 30 de Abr. de 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
Satisfied condition in Fr5 Satisfied condition in Fr4 Satisfied condition in Fr3 Satisfied condition in Fr2 Satisfied condition in Fr1
  13 comentarios
Walter Roberson
Walter Roberson el 30 de Abr. de 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")

Iniciar sesión para comentar.

Más respuestas (2)

Ahmad
Ahmad el 30 de Abr. de 2022
Editada: Ahmad el 30 de Abr. de 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
Alberto Cuadra Lara el 30 de Abr. de 2022
Editada: Alberto Cuadra Lara el 30 de Abr. de 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
Satisfied condition in Fr5 Satisfied condition in Fr4 Satisfied condition in Fr3 Satisfied condition in Fr2 Satisfied condition in Fr1
fprintf('Execution time %f\n', toc)
Execution time 0.029996
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
Satisfied condition in Fr5 Satisfied condition in Fr4 Satisfied condition in Fr3 Satisfied condition in Fr2 Satisfied condition in Fr1
fprintf('Execution time %f\n', toc)
Execution time 0.005146

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by