How to change matrix values in matlab without loop

Hi,
Assuming that I have the matrix A:
A =
0.6638 0.9134 0.2785
0.9058 0.3999 0.5469
0.1270 0.0975 0.9168
I want to change the matrix value by the following condition without loops:
A(i,j) = (a)^2 if i=j and i=n+1-j
2*a otherwise
I read about sub2ind, It helped mw with the main diagonal but not the second diagonal (for i=n-j+1).
Any one can help me please?
Thanks in advance :)

2 件のコメント

KSSV
KSSV 2016 年 11 月 19 日
What is n? Does a stand for aij?
Idan Cohen
Idan Cohen 2016 年 11 月 19 日
In my example n=3, but I need for n that stands for the matrix size

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

回答 (1 件)

Roger Stafford
Roger Stafford 2016 年 11 月 19 日

0 投票

Assume A is n by n in size.
A((1:n)+n*(0:n-1)) = a^2;
A(n*(1:n)-(0:n-1)) = 2*a
Note: If n is odd, the two diagonals meet at the center. Depending on which rule is to have preference, you can do the above or reverse the order:
A(n*(1:n)-(0:n-1)) = 2*a
A((1:n)+n*(0:n-1)) = a^2;

カテゴリ

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

タグ

質問済み:

2016 年 11 月 19 日

コメント済み:

2016 年 11 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by