Size of The Matrix in Diagonal Addition

2 次查看(过去 30 天)
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by