フィルターのクリア

Constructing a matrix from several diagonal matrixes (the code is included)

1 回表示 (過去 30 日間)
Lily
Lily 2012 年 9 月 20 日
Hi
I'm trying to construct a matrix from 5 diagonal matrixes that are repriceted as M (see code below). The first M I get is [0 0 0; 0 0 0; 1 0 0] Second M is [0 0 0; 6 0 0; 0 3 0] Third M is [4.667 0 0; 0 4.6667 0; 0 0 1.667] Fourth M is [0 5.5 0; 0 0 5.5; 0 0 0] Fifth M is [0 0 4; 0 0 0; 0 0 0]
The matrix I'm tryging is made from these 5 M matrices made. I trying to take the values that are larger then zero from these 5 M matrices and construct one matrix or [4.6667 5.5 4; 6 4.6667 5.5; 1 3 1.6667]. But I don't know know to do that. Can you please help me?
NOTE: That I've improved the question a bit, I hope it helps to explain what I'm trying to do.
close all; clear all; clc;
A=[5 5 5; 5 5 5; 2 2 2];
B=[-1 2 -1 1 -1];
[x,y] = size(A);
[x1,y1] = size(B);
T = ones(x,y);
M0 = ones(x,y);
c = x-1;
v = -c:1:c;
ii = 1;
for vv = min(v):max(v)
sumd(ii) = sum(diag(M0,vv));
ii = ii+1;
end
differance = B./sumd
a = 1;
for dd = min(v):max(v)
Mcal = (diag(A,dd)+differance(a));
[x2,y2]=size(Mcal);
M = diag(Mcal,dd) %diagnonal matrices
a=a+1;
end
  4 件のコメント
Matt Fig
Matt Fig 2012 年 9 月 20 日
編集済み: Matt Fig 2012 年 9 月 20 日
Lilja, what are the 5 diagonal matrices? I only see A and B, and others made from them. Please be more specific in your questions.
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 20 日
編集済み: Azzi Abdelmalek 2012 年 9 月 20 日
I still didn't understand how to construct M from M1,M2,... you did'nt tell anything about it

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

採用された回答

Matt Fig
Matt Fig 2012 年 9 月 20 日
編集済み: Matt Fig 2012 年 9 月 20 日
Replace your second loop with this:
M = zeros(size(A));
for dd = min(v):max(v)
Mcal = (diag(A,dd)+differance(a));
[x2,y2]=size(Mcal);
M = M + diag(Mcal,dd);
a=a+1;
end
M
That at least gives you your M. Now you can start improving the code efficiency-wise.
  1 件のコメント
Lily
Lily 2012 年 9 月 20 日
編集済み: Lily 2012 年 9 月 20 日
OMG you are a genius Matt! Thank you so much :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by