Size of The Matrix in Diagonal Addition
1 view (last 30 days)
Show older comments
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 Comment
Walter Roberson
on 23 Nov 2019
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.
Accepted Answer
Walter Roberson
on 23 Nov 2019
Edited: Walter Roberson
on 23 Nov 2019
b = n*s - 6*n + 12
The 12 is the constant size of C. The 6 is the overlap.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!