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

1 ビュー (過去 30 日間)
Chaudhary P Patel 2022 年 4 月 30 日
コメント済み: Walter Roberson 2022 年 4 月 30 日
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 件のコメント表示非表示 1 件の古いコメント
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 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 件のコメント表示非表示 12 件の古いコメント
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 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
##### 1 件のコメント表示非表示 なし
Chaudhary P Patel 2022 年 4 月 30 日
Fr is the variable vector, do not put in a matrix form.

サインインしてコメントする。

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

サインインしてコメントする。

### カテゴリ

Find more on Matrix Indexing in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by