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

조회 수: 1 (최근 30일)
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
Walter Roberson
Walter Roberson 2022년 4월 30일
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 2022년 4월 30일
@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.

댓글을 달려면 로그인하십시오.

채택된 답변

Alberto Cuadra Lara
Alberto Cuadra Lara 2022년 4월 30일
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
Walter Roberson
Walter Roberson 2022년 4월 30일
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")

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Ahmad
Ahmad 2022년 4월 30일
편집: Ahmad 2022년 4월 30일
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 2022년 4월 30일
편집: Alberto Cuadra Lara 2022년 4월 30일
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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by