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

 採用された回答

Geoff Hayes
Geoff Hayes 2014 年 9 月 28 日
編集済み: Geoff Hayes 2014 年 9 月 28 日

0 投票

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 件のコメント

Basheer
Basheer 2014 年 9 月 28 日
Yes, I am wanna find D depending on A,B,C with different m.
thank you, the code is great,but I didnt understand the steps 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
hope that you can explain it.
Image Analyst
Image Analyst 2014 年 9 月 28 日
It's just a very ad hoc way to build the matrix you said you wanted. Since you didn't really give any recipe to build it, he just came up with something that would do the job. Whether it works for other initial matrices, perhaps of different sizes, and for other desired output matrices, we can't know since you didn't state how generalized this needs to be, or any way/recipe/algorithm to get a generalized result.
Basheer
Basheer 2014 年 9 月 28 日
編集済み: Image Analyst 2014 年 9 月 28 日
The output of this code is what I am looking for. its work properly. but I couldn't understand the lines
D(1,1:size(A,2)) = A;
D(end,m+1:end) = C;
thanks a lot
Image Analyst
Image Analyst 2014 年 9 月 28 日
That's just standard basic MATLAB code like you must already know, if you know how to use MATLAB. The first line says that for row 1, assign columns 1 to size(A,2) (which is the number of columns in A) to the same values that A has.
The second line says for the last row (which we can use the shortcut term "end" to refer to), go from column m+1 to the last column of D and assign the respective values of C to those elements.
In general if you have
D(row, col1:col2) = rowVector;
it will make these assignments
D(row, col1) = rowVector(1);
D(row, col1+1) = rowVector(2);
D(row, col1+2) = rowVector(3);
.....and so on up to the final element....
D(row, col2) = rowVector(end);
Basheer
Basheer 2014 年 9 月 29 日
Dear Image Analyst, thank you for the great explanation, but I still confused about the two expressions: 1- size(A,2) 2- and size(B,2) for D(1+k,k:k+size(B,2)-1)=B ?? thanks
Geoff Hayes
Geoff Hayes 2014 年 9 月 29 日
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 2014 年 9 月 29 日
Dear Geoff, Thanks a lot. now it is very clear.
thanks again.

サインインしてコメントする。

その他の回答 (2 件)

Image Analyst
Image Analyst 2014 年 9 月 28 日

0 投票

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 件のコメント

Basheer
Basheer 2014 年 9 月 28 日
thank you for your comment. yes I don't need the for loop. your answer helped me as well.

サインインしてコメントする。

Andrei Bobrov
Andrei Bobrov 2014 年 9 月 28 日

0 投票

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 件のコメント

Basheer
Basheer 2014 年 9 月 29 日
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

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2014 年 9 月 28 日

コメント済み:

2014 年 9 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by