How can I convert A matrix to B matrix below?

1 回表示 (過去 30 日間)
Rengin
Rengin 2014 年 3 月 18 日
コメント済み: Image Analyst 2014 年 3 月 21 日
I want to convert
A=[a11 a12 a13;
a22 a22 a23;
a31 a32 a33]
to
B=[a11 a11 a11;
a11 a22 a22;
a11 a22 a33]
Thank you!

採用された回答

Jos (10584)
Jos (10584) 2014 年 3 月 18 日
B = eye(size(A)).*A
B = cumsum(B,1) + cumsum(B,2) - B
  1 件のコメント
Rengin
Rengin 2014 年 3 月 21 日
Thankssss!!!

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

その他の回答 (4 件)

Image Analyst
Image Analyst 2014 年 3 月 18 日
Try it this way:
% This robust code works even if A is not square.
A = randi(99, 7,10) % Sample data.
B = A; % Initialize.
for k = 1 : min(size(A)) % Go only as far as we can.
B((k+1):end, k) = A(k,k); % Send value down along column.
B(k, (k+1):end) = A(k,k); % Send value along row to the right.
end
B % Print out to command window.
  3 件のコメント
Rengin
Rengin 2014 年 3 月 21 日
Thank you for your answer but i couldn't get the results which I wanted to see. In my matrix except my diagonal elements, all elements are "0". When I wrote your code, my matrix remained same.
Image Analyst
Image Analyst 2014 年 3 月 21 日
That would have been good to state at the start, though it doesn't really matter to the code whether the off diagonals are zero or not. The code still works. Here's proof, where I make A a diagonal matrix:
% Now, do again for A being diagonal:
numberOfRows = 7;
A = randi(99, numberOfRows, numberOfRows) % Sample data.
A = A .* eye(numberOfRows) % Zero out everything except the diagonal.
B = A; % Initialize.
for k = 1 : min(size(A)) % Go only as far as we can.
B((k+1):end, k) = A(k,k); % Send value down along column.
B(k, (k+1):end) = A(k,k); % Send value along row to the right.
end
B % Print out to command window.
A =
31 0 0 0 0 0 0
0 45 0 0 0 0 0
0 0 8 0 0 0 0
0 0 0 9 0 0 0
0 0 0 0 15 0 0
0 0 0 0 0 35 0
0 0 0 0 0 0 42
B =
31 31 31 31 31 31 31
31 45 45 45 45 45 45
31 45 8 8 8 8 8
31 45 8 9 9 9 9
31 45 8 9 15 15 15
31 45 8 9 15 35 35
31 45 8 9 15 35 42
Not sure what you meant by the matrix remaining the same. Obviously B is not the same as A and is what you wanted. And is exactly the same output as what Jos's code produces so I'm puzzled as to why you say mine didn't give the results you wanted but Jos's does. So it works fine. But whatever, if the cumsum method is easier for you, then fine.

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


Roger Stafford
Roger Stafford 2014 年 3 月 18 日
Try this:
B = repmat(diag(A),size(A,2));
B = triu(B)+tril(B.',-1);
  1 件のコメント
Rengin
Rengin 2014 年 3 月 21 日
Thank you for your answer but when I wrote your code, I got a such warning: Matrix dimensions must agree.

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


Joseph Cheng
Joseph Cheng 2014 年 3 月 18 日
編集済み: Joseph Cheng 2014 年 3 月 18 日
Pretty much how you just typed it?
B = [A(1,1) A(1,1) A(1,1);...
A(1,1) A(2,2) A(2,2);...
A(1,1) A(2,2) A(3,3)];
or is there more to your question?
  1 件のコメント
Rengin
Rengin 2014 年 3 月 18 日
I am not asking it! a11,a22,....ann my diagonal elements. Let's say I have 100x100 sized matrix and for a72-72, the rows from 72 to 100 in 72th column and the columns from 72 to 100 in 72th row have to be a72-72

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


Andrei Bobrov
Andrei Bobrov 2014 年 3 月 21 日
編集済み: Andrei Bobrov 2014 年 3 月 21 日
z = diag(A);
ii = 1:numel(z);
B = z(bsxfun(@min,ii,ii'));

カテゴリ

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