How can I make the entries of a matrix be a square root of the sum of their column number squared and row number squared ?

2 ビュー (過去 30 日間)
Hello everyone, My question is: can I create a matrix of which entries are the square roots of the sum of its x and y position coordinates ( column number and row number respectively ) squared ? For example, for 3x3 matrix, it would look like: X = [ sqrt(2) sqrt(5) sqrt(10) ; sqrt(5) sqrt(8) ... ]. Is there a way of getting it in an automated way ? Thank you.

採用された回答

Stephen23
Stephen23 2017 年 6 月 26 日
編集済み: Stephen23 2017 年 6 月 26 日
Method one: ndgrid:
>> Rn = 4;
>> Cn = 3;
>> [Rv,Cv] = ndgrid(1:Rn,1:Cn);
>> sqrt(Rv.^2+Cv.^2)
ans =
1.4142 2.2361 3.1623
2.2361 2.8284 3.6056
3.1623 3.6056 4.2426
4.1231 4.4721 5.0000
Method two: bsxfun:
>> fun = @(r,c)sqrt(r.^2+c.^2);
>> bsxfun(fun,(1:Rn)',1:Cn)
ans =
1.4142 2.2361 3.1623
2.2361 2.8284 3.6056
3.1623 3.6056 4.2426
4.1231 4.4721 5.0000
Method three: implicit expansion:
>> sqrt((1:Rn).'.^2 + (1:Cn).^2)
ans =
1.4142 2.2361 3.1623
2.2361 2.8284 3.6056
3.1623 3.6056 4.2426
4.1231 4.4721 5.0000

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by