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

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 Comments

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 ?
@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.

Sign in to comment.

 Accepted Answer

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 Comments

length() of a 2D matrix is defined as:
0 if isempty(matrix)
max(size(matrix)) if ~isempty(matrix)
Your FR happens to be 5 x 5, so length() is 5 either way... but it is not at all clear whether you are intending to talk about rows or columns when you talk about length() there.
for any given variables such as Fr1, i want to test that there is at least one element that is less than the corresponding Fs element
FLAG(i) = any(FR(:, i) < Fs);
tests what you want tested. FR(:,i) will be a column vector, and Fs will be a column vector the same size, and using < between column vectors the same size results in testing corresponding elements. So the posted code should work.
@Walter Roberson sir, i am agree with @Alberto Cuadra Lara sir's code but the problem is Fr is a column vector and that is variable. For example i have taken here as Fr1 to Fr5 but it may be Fr14000.
In my actual problem Fr is calculated from the above steps. So i have to call and take it individual element one by one.
You are mistaken:
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];
size(FR)
ans = 1×2
5 5
The individual Fr* variables are column vectors, but the "," operator is used between those column vectors inside the [] and that means that those are to be horizontally concatenated. The result would be (length of Fr* vector) by (number of Fr vectors)
For the last several yes (since about R2015b or so) if you were to create the FR array the way Alberto shows, then
FLAG = any(FR < Fs, 1);
would calculate the results in a vectorized way, without having to use a loop.
The most difficult part of it is getting all those Fr* numbered variable into a single numeric array. You probably should not be using numbered variables like that; see http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
In my actual problem Fr is calculated from the above steps. So i have to call and take it individual element one by one.
If you are generating the Fr* variables step by step instead of having them all available at the same time, then for any given one, FrN, the solution is just
FLAG = any(FrN < Fs)
We told you before not to use eval(). And in the context you have, it is pointless to use eval() there, unless Ktts*l or Utdelt* are the names of scripts to run... and if they are then you have other problems.
Cr(i)=any(f_s(j,i+1)<=f_r(j,1));
You are comparing scalar to scalar there; there is no point in using any() there.
if f_s(j,i+1)<=f_r(j,1)
Utdelt(:,i+1)=Utdelt(:,i+1);
else
Utdelt(:,i+1)=Utdelt(:,i);
end
@Walter Roberson sir, but i have to check all 6 rows of FR with Fs firstly, If atleast one of the 6 elements will satisfy the condition then we can right , not from a single element check.
utdelt(:,i+1)=utdelt(:,i+1)
You removed your comments that had your existing code, so I cannot refer to it.
We have shown you had to vectorize the test, any(Fr < Fs) .
If you insist on using a for loop then:
found_one = false;
for j = 1 : 6
if f_s(j,i+1)<=f_r(j,1)
found_one = true;
break;
end
end
if found_one
Utdelt(:,i+1)=Utdelt(:,i+1);
else
Utdelt(:,i+1)=Utdelt(:,i);
end
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")

Sign in to comment.

More Answers (2)

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
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

Community Treasure Hunt

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

Start Hunting!