问题
I have two matrices (5_by_1), say A=[5 rows,1 column] and B=[5 rows, 1 column] if I do plot(A,B), I will create a large matrix C=[5 rows,5 columns] wright?!
Now I would like to create this large matrix without plot it. I want this matrix directly. thank you.
For example A=[1 2 3 4 5 ]
and B=[3 4 2 1 4]
c=
0 1 0 0 0
1 0 0 0 1
0 0 1 0 0
0 0 0 1 0
回答1:
This should work:
a = [1 2 3 4 5];
b = [3 4 2 1 4];
c = flipud(sparse(b,a,1,4,5));
If you want to see the full c:
full(c)
or if you have a bigger version:
c = flipud(sparse(b,a,1,max(b),max(a)));
The flipud
command is to flip the matrix upside down.
Hope this helps =)
EDIT:
"Shift" the matrices, so that your lowest value is in (1,1) (before you flip it). The structure will be correct, but the origin won't be easy to spot.
a_1 = floor(a - min(a)) + 1; % floor if you don't have integers.
b_1 = floor(b - min(b)) + 1;
c = flipud(sparse(b_1,a_1,1,max(b_1),max(a_1)));
来源:https://stackoverflow.com/questions/16495360/generate-plot-matrix-from-two-different-matrices-in-matlab