i want to generate a matrix that each element is the sum of its row and column ive tried usin the henkel command but it didnt work is there an alternative or more simple solution thx

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 12 月 11 日

0 投票

A=zeros(3,4);
[ii,jj]=ind2sub(size(A),1:numel(A));
A(1:end)=sum([ii' jj'],2)

3 件のコメント

mohamed
mohamed 2013 年 12 月 11 日
thx very much but i dont understand some of the codes like ind2sub abd nume
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 12 月 11 日
A=[20 4;3 5]
numel(A) % is number of element in A
ans=
4
A(1)
A(2)
A(3)
A(4)
1,2,3 and 4 are linear indices. to get subscripts use ind2sub
ii=[1 2 1 2] rows
jj=[1 1 2 2] column
Look at
doc numel
doc ind2sub
mohamed
mohamed 2013 年 12 月 12 日
thank you

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2013 年 12 月 11 日
編集済み: Andrei Bobrov 2013 年 12 月 11 日

0 投票

s = [3 4];% let s - size new matrices
A = hankel(2:s(1)+1,s(1)+1:sum(s));
or
A = bsxfun(@plus,(1:s(1))',1:s(2));
or
s = [3 4];
[ii,jj] = ndgrid(1:s(1),1:s(2));
A = ii + jj;

1 件のコメント

mohamed
mohamed 2013 年 12 月 12 日
thx very much

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

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

質問済み:

2013 年 12 月 11 日

コメント済み:

2013 年 12 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by