在特殊情况下创建更大的矩阵 [英] Create a larger matrix in special case
问题描述
我有两个向量:
A=[1 2 3 4]
B=[3 5 3 5]
我想从这些向量中找到一个矩阵,如下所示:
I want to find a matrix from these vectors like this:
您可以假设c
是plot matrix
,其中x轴是A
,而y-axis
是B
:
you can suppose that c
is a plot matrix
, where the x-axis is A
and y-axis
is B
:
c =
0 4 0 4
3 0 3 0
0 0 0 0
0 0 0 0
或:
c1=
0 1 0 1
1 0 1 0
0 0 0 0
0 0 0 0
我的问题是因为我的向量很大,所以如何自动创建它.
My question is how to create it automatically because I have large vectors.
推荐答案
我假设A
和B
是坐标,并且您想以矩阵形式绘制"绘图,所以请尝试以下操作:
I'm assuming that A
and B
are coordinates, and you want to "draw" the plot in matrix form, so try this:
c = flipud(full(sparse(B, A, B)));
我添加了flipud
来向上调整y轴的正方向.
I added flipud
to adjust the positive direction of the y-axis upwards.
或者,您可以使用以下方法获得二进制矩阵:
Alternatively, you can obtain a binary matrix using this:
c1 = flipud(full(sparse(B, A, ones(size(A)))));
重要提示:要使此解决方案有效,A
和B
必须包含正整数.试图绘制"具有非正或非整数位置的矩阵是没有意义的.
Important: for this solution to work, A
and B
must contain positive integer values. There is no sense in try to "plot" a matrix with non-positive or non-integer positions.
A = 1:4; B = [3, 4, 3, 4];
c = flipud(full(sparse(B, A, B)))
c1 = flipud(full(sparse(B, A, ones(size(A)))))
结果是:
c =
0 4 0 4
3 0 3 0
0 0 0 0
0 0 0 0
c1 =
0 1 0 1
1 0 1 0
0 0 0 0
0 0 0 0
这篇关于在特殊情况下创建更大的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!