Compare two vectors in matlab n*m

4 ビュー (過去 30 日間)
marwa hajji
marwa hajji 2021 年 10 月 5 日
コメント済み: Rik 2021 年 10 月 5 日
A=[1 2 3 4 5 6 7 8 9 10]
B=[4]
I want to compare these two vectors one by one. I want MATLAB to write as a result of it. for example,
with A(1)(1 here) If 1=B(1) the result should be 1/2, and if A(n)<B(1) the result should be 1, and A(n)>B(1) = 0.
It should compare all elements one by one and output its index in a new vector.

採用された回答

Rik
Rik 2021 年 10 月 5 日
You mean like this?
A=[1 2 3 4 5 6 7 8 9 10];
B=4;
result=zeros(size(A));%pre-allocate
L= A<B ;result(L)=1;
L= A==B;result(L)=1/2;
disp(result)
1.0000 1.0000 1.0000 0.5000 0 0 0 0 0 0
  2 件のコメント
marwa hajji
marwa hajji 2021 年 10 月 5 日
big thanks
Rik
Rik 2021 年 10 月 5 日
You're welcome. Did either of our answers solve your problem? If so, please consider marking one of them as accepted answer. If the other helped as well, please consider giving that one an up vote.
If neither solved the problem, feel free to post a comment with your remaining issues.

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

その他の回答 (1 件)

KSSV
KSSV 2021 年 10 月 5 日
A=[1 2 3 4 5 6 7 8 9 10] ;
B=4 ;
idx = A >= B
idx = 1×10 logical array
0 0 0 1 1 1 1 1 1 1
  1 件のコメント
marwa hajji
marwa hajji 2021 年 10 月 5 日
thank you

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by