How to update matrix values using algorithm based on position?

1 回表示 (過去 30 日間)
Clark
Clark 2012 年 9 月 22 日
Suppose I've already create a square matrix, A=zeros(n).
Now let's say, for every position, A(i,j), I want to update the value to 1 / (i + j^2), can I do this easily?
Thanks!

採用された回答

Matt Fig
Matt Fig 2012 年 9 月 22 日
編集済み: Matt Fig 2012 年 9 月 22 日
The straightforward way is to just use a loop:
A = zeros(n);
for ii = 1:n
for jj = 1:n
A(ii,jj) = 1/(ii+jj^2);
end
end
Here is another way to do it:
B = bsxfun(@(x,y) 1./(x+y.^2),(1:n).',1:n)
  2 件のコメント
Clark
Clark 2012 年 9 月 22 日
Thanks. Can it be done without a loop?
Clark
Clark 2012 年 9 月 22 日
Thanks!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by