how to compare elements in an array respectively

39 ビュー (過去 30 日間)
omar mahallawy
omar mahallawy 2019 年 3 月 4 日
回答済み: Sulaymon Eshkabilov 2019 年 7 月 25 日
i have a static array and 13 variable arrays, each array is the same in terms of size, but now i need to know how can i compare
element 1 with element 1
element 2 with element 2
and so on.....
then goes into an if condition
attached here is what i have so far
  2 件のコメント
KSSV
KSSV 2019 年 3 月 5 日
Give one example......and your expectations.
omar mahallawy
omar mahallawy 2019 年 3 月 5 日
Max=[ 29 17 23 29] %static vector
A =7 13 16 28 %from for loop
B = 9 15 18 30 %from for loop
comparing vectors A and B to vector Max
A is accepted becasue non of it's elements exceed the specified elements in vector Max
B is rejected Becasue 30>29

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

回答 (2 件)

Ramnarayan Krishnamurthy
Ramnarayan Krishnamurthy 2019 年 6 月 21 日
If I understand correctly, you are comparing the arrays element wise and then deciding based on a condition. One approach could be the following:
% Compare elementwise between A and Max; then check if any of the values is non zero i.e. 1
any(A > Max)
% Then use the above as a condition in an if loop to accept or reject the array
if (~any(A>Max)) % then accept
Depending on how you store the 13 variable arrays (A,B, etc) you may be able to accomplish this in a loop
HTH

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2019 年 7 月 25 日
Hi,
Here is a short anwer:
Max_vs_AB = any(Max>A & Max>B);
A_vs_B = any(A>B);
if Max_vs_AB ==1 && A_vs_B==1
fprintf('Accepted matrix is B \n')
disp(B)
elseif Max_vs_AB == 0
fprintf('Accepted matrix is Max \n')
disp(Max)
else
fprintf('Accepted matrix is A \n')
disp(A)
end

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by