フィルターのクリア

Creating a matrix containing binary vectors as elements

1 回表示 (過去 30 日間)
David
David 2012 年 9 月 22 日
Based on the dimension n, i want to create a matrix containing binary vectors:
For example with n=1:
A=[[-1],[0],[1]]'
n=2:
A=[[-1,-1],[-1,0],[-1,1],[0,-1],...,[1,1]]
n=3:
A=[[-1,-1,-1,-1],[-1,-1,-1,0],[-1,-1,-1,1],[-1,-1,0,-1],[-1,-1,0,0],...,[1,1,1,1]]'
Since the dimension is the input variable, I don't know how many while- or for-commands i need in advance.
Any suggestions?
  1 件のコメント
Star Strider
Star Strider 2012 年 9 月 22 日
編集済み: Star Strider 2012 年 9 月 22 日
It's not obvious from your question what you want to do.
How are you creating the elements of your vectors?
Is ‘A’ different for each value of n or are you dynamically creating A?
For a start, I suggest you explore creating ‘A’ as cell or perhaps structure data. They may do what you want.

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

採用された回答

Rick Rosson
Rick Rosson 2012 年 9 月 22 日
編集済み: Rick Rosson 2012 年 9 月 22 日
N = 4;
A = zeros(3^N,N);
for k = 1:N
r = N - k;
v = [ -1*ones(3^r,1) ; zeros(3^r,1) ; ones(3^r,1) ];
A(:,k) = repmat(v,3^(k-1),1);
end
  1 件のコメント
David
David 2012 年 9 月 23 日
Thank you Rick.
That's exactly what i was looking for.
David

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

その他の回答 (0 件)

カテゴリ

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