フィルターのクリア

How to efficiently encode two values as a unique entitiy

4 ビュー (過去 30 日間)
balandong
balandong 2018 年 11 月 23 日
回答済み: Andrei Bobrov 2018 年 11 月 23 日
Dear all,
With respect to the above subject, I wonder if we can make the following code more efficient and compact (reduce the ifelse)..
A=3;
B=3;
C=[];
if A==1 && B==1
C=1;
elseif A==1 && B==2
C=2;
elseif A==1 && B==3
C=3;
elseif A==2 && B==1
C=4;
elseif A==2 && B==2
C=5;
elseif A==2 && B==3
C=6;
elseif A==3 && B==1
C=7;
elseif A==3 && B==2
C=8;
elseif A==3 && B==3
C=9;
end
In addition, I also wonder if we can scale it up for A=[2;2;3;1] and B= [1;1;3;1]
Thanks in advance

採用された回答

Stephen23
Stephen23 2018 年 11 月 23 日
編集済み: Stephen23 2018 年 11 月 23 日
M = [1,2,3;NaN,4,5;6,7,8];
C = M(A,B)
"In addition, I also wonder if we can scale it up for A=[2;2;3;1] and B= [1;1;3;1]"
arrayfun(@(a,b)M(a,b),A,B)
  1 件のコメント
balandong
balandong 2018 年 11 月 23 日
Dear Stephen,
Thanks for the quick reply, your solution work like a charm

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2018 年 11 月 23 日
in your case:
>> A=[3;3;3;1]; B= [1;1;3;1];
f = @(A,B) 3*(A(:) - 1) + B(:);
C = f(A,B)
C =
7
7
9
1
>>

カテゴリ

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

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by