build m x m matrix
12 views (last 30 days)
Show older comments
Hello, I wanna build a matrix of m nods. I have some questions:
1-
if F=[-3 2 -3]
for I=1:m;end
B=eye(I)*F; ---------(1)
the problem here is F in eq (1) is not allowed in matlab, what I have to do to built that matrix which affected by different values of m?
2-
A=[5 -3];C=[-3 4];
I wanna use use the three matrices (A,B,C), to get the result to be like
D= [ 5 -3 0 0 0;
-3 2 -3 0 0;
0 -3 2 -3 0;
0 0 -3 2 -3;
0 0 0 -3 4 ]
What do u suggest??
thanks
0 Comments
Accepted Answer
Geoff Hayes
on 28 Sep 2014
Edited: Geoff Hayes
on 28 Sep 2014
Basheer - as F is a 1x3 matrix, the only multiplication with eye(I) that will succeed is when I==1 (since that will be a 1x1 matrix multiplied against the 1x3 matrix F).
What are you attempting to achieve by creating this B? Is it the output of D that you want, and so when you supply a different m, you will get different D matrices?
A = [5 -3];
B = [-3 2 -3];
C = [-3 4];
m = 3;
D = zeros(m+2,size(A,2)+m);
D(1,1:size(A,2)) = A;
D(end,m+1:end) = C;
for k=1:m
D(1+k,k:k+size(B,2)-1)=B;
end
So D becomes
D =
5 -3 0 0 0
-3 2 -3 0 0
0 -3 2 -3 0
0 0 -3 2 -3
0 0 0 -3 4
7 Comments
Geoff Hayes
on 29 Sep 2014
Basheer - the statement size(A,2) returns the number of columns of A since the number of columns is given by the second dimension. size(B,2) returns the number of columns for B. The results for each are 2 and 3 respectively.
The line of code
D(1,1:size(A,2)) = A;
means that we wish to replace columns 1 through 2, this is the 1:size(A,2), of row 1, with the matrix A. And we can do this since we are replacing a 1x2 "portion" of D with the 1x2 matrix A.
The line of code
D(1+k,k:k+size(B,2)-1)=B;
means that we wish to replace columns k through k+3-1, this is the k:k+size(B,2)-1, of row 1+k with the matrix B. And we can do this since we are replacing a 1x3 "portion" of D with the 1x3 matrix B. So if k is 1, then our line of code becomes
D(1+1,1:1+3-1)=B;
or
D(2,1:3)=B;
So we are placing the first three columns of D, given by the 1:3, of the second row, with B.
More Answers (2)
Image Analyst
on 28 Sep 2014
First of all, in #1, the for loop does nothing. There is an end on the same line and nothing in between so the for loop just iterates but does no commands because no commands are inside it.
Secondly, in your formula 1, I think you want to use .* (element by element multiplication) instead of * (matrix multiplication), and it would only work if F has "I" rows. Since eye(I) is square, F must be have the same number of rows as eye(I) has columns.
For #2, I'm not seeing how matrix D was constructed from A, B, and C. Please explain.
Andrei Bobrov
on 28 Sep 2014
F = [-3 2 -3];
A=[5 -3];
C=[-3 4];
out = full(spdiags(ones(5,1)*F,-1:1,5,5));
out(1:2) = A;
out(end-1:end) = C;
See Also
Categories
Find more on Logical 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!