build m x m matrix

12 views (last 30 days)
Basheer
Basheer on 28 Sep 2014
Commented: Basheer on 29 Sep 2014
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

Accepted Answer

Geoff Hayes
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
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.
Basheer
Basheer on 29 Sep 2014
Dear Geoff, Thanks a lot. now it is very clear.
thanks again.

Sign in to comment.

More Answers (2)

Image Analyst
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.
  1 Comment
Basheer
Basheer on 28 Sep 2014
thank you for your comment. yes I don't need the for loop. your answer helped me as well.

Sign in to comment.


Andrei Bobrov
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;
  1 Comment
Basheer
Basheer on 29 Sep 2014
Thank you Andrei, could you explain the (spdiags(ones(5,1)*F,-1:1,5,5) I feel confused about -1:1,5,5 what is -1:1,5,5 mean? thanks

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!