"plus/minus" sign operator

17 ビュー (過去 30 日間)
frwmetric
frwmetric 2013 年 5 月 17 日
Hi
I have a row matrix which is like n=[x,y,z] I need to consider plus and minus signs at the same time and get a permutations of them. In conclusion I need that solutions
n=[x,y,z]
n=[-x,y,z]
n=[x,-y,z]
n=[x,y,-z]
n=[-x,-y,z]
n=[-x,y,-z]
n=[x,-y,-z]
n=[-x,-y,-z]

採用された回答

Iain
Iain 2013 年 5 月 17 日
It depends what you want to do with it.
M = [1 1 1; 1 1 -1; 1 -1 1; 1 -1 -1; -1 1 1; -1 1 -1; -1 -1 1; -1 -1 -1];
Out = (M * Row')' ;
Each row of Out is what you want.
You might want to programatically generate M if you have more columns on your row.
  2 件のコメント
Iman Ansari
Iman Ansari 2013 年 5 月 17 日
[a b c]=meshgrid([-1 1]);
M=[a(:) b(:) c(:)];
frwmetric
frwmetric 2013 年 5 月 17 日
thank you...

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

その他の回答 (1 件)

C.J. Harris
C.J. Harris 2013 年 5 月 17 日
Not exactly elegant, but it works:
A = [1 2 3];
B = (dec2bin(0:2^length(A)-1) - '0');
C = sum(B .* repmat(A, 2^length(A), 1), length(A)) + sum(~B .* repmat(-A, 2^length(A), 1), length(A));

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by