Generating A(i,j) = [i; j]

20 ビュー (過去 30 日間)
Jeong Ho
Jeong Ho 2012 年 12 月 4 日
Hi, is there a clean way to generate a matrix A(i,j) = [i; j], especially without resorting to loops? Thank you so much!
  1 件のコメント
Jonathan Epperl
Jonathan Epperl 2012 年 12 月 4 日
No, that would not be a matrix, since you're assigning a vector to the element at (i,j). Do you want to create a 3-dim array, or a cell array, or a blockmatrix? Please modify your question accordingly.

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

採用された回答

Matt Fig
Matt Fig 2012 年 12 月 4 日
編集済み: Matt Fig 2012 年 12 月 4 日
You want each element of your matrix to be a two element vector?
That is not a matrix, but it does sound like a cell array.
C = num2cell(cumsum(ones(5),2));
C = cellfun(@(x,y) [x;y],C,C.','Un',0)

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 12 月 4 日
編集済み: Walter Roberson 2012 年 12 月 4 日
No. Numeric matrices cannot store multiple items per location.
Cell arrays can store vectors in each location, but then A(i,j) would not be the vector [i;j] and would instead be the 1 x 1 cell array that contained within it the vector [i;j]
Perhaps you would like a 3D array instead.
[P, Q] = ndgrid(1:5, 1:7); %sample bounds
A = cat(3, P, Q);
You could convert to a cell array:
mat2cell(A, ones(1,size(A,1)), ones(1,size(A,2)), 2)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by