Size of The Matrix in Diagonal Addition

조회 수: 1 (최근 30일)
Chris Dan
Chris Dan 2019년 11월 23일
댓글: Chris Dan 2019년 11월 24일
Hello, I have to perform diagnoal matrix addition for one of my projects. The code is running successfully but the problem is that the resulting matrix is bing made of a bigger size than I need it to be.
here is my code :
% Development of an algorithim to add matrices diagnoally with 6 elements
% in common(under development )
% Size of S can be changed, but it should not be less than 12x12
S(1).model_data = sparse( rand( 13, 13 )) ;
S(2).model_data = sparse( rand( 13, 13 )) ;
S(3).model_data = sparse( rand( 13, 13 )) ;
% Size of C is fixed, it cannot be changed
C = sparse( rand( 12, 12 )) ;
s = size(S(1).model_data,1); % size of matrix in a struct
n = size(S,2);% number of matrices in the struct
b = (s+(s-1)+(n-2)*(s-1))+(size(C,2) -1); %PROBLEM HERE % size of resulting matrix(T)
T = zeros(b,b); % resulting matrix
counter = 0;
aa = 2;
for k = 1:1:(n+1)
if (k == 2)
y = size(C,2);
else
y = s ;
end
for i = 1:1:y % row
for j = 1:1:y % column
if (k> aa)
m = (size(C,2)-s) + ((k-1)*(s-6)); %after the C matrix
else
m =(k-1)*(s-6); % before the C matrix
end
if (k == aa)
T(i+m,j+m)= T(i+m,j+m) +C(i,j);
counter = 1;
else
T(i+m,j+m)= T(i+m,j+m) +(S(k-counter).model_data(i,j));
end
end
end
end
The highlighted variable b is giving me problems, does nay body knows how to fix it ?
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 11월 23일
You have not defined in the comments how the overlap is to take place, where C is to be placed relative to the S, and where the S are to be placed, and what the overlap scheme is.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2019년 11월 23일
편집: Walter Roberson 2019년 11월 23일
b = n*s - 6*n + 12
The 12 is the constant size of C. The 6 is the overlap.
  댓글 수: 1
Chris Dan
Chris Dan 2019년 11월 24일
Great!! Thanks for the answer :) :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by