MATLAB矩阵范围分配 [英] MATLAB matrix range assignment
问题描述
是否可以将范围分配给矩阵. 如果您将下面的零矩阵视为绘制的网格":
Is it possible to assign ranges to a matrix. If you consider the below zeros matrix as a 'grid' for plotting:
R = zeros(5,8);
R =
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
那么您可以将此矩阵视为网格,以便将每个x轴零都视为一个范围吗?例如,R(5,1)
是范围0-0.1 seconds
. R(5,2)
是范围0.1-0.2 seconds
等.
So can you treat this matrix as a grid so each x-axis zero can considered as a range? for example R(5,1)
is a range 0-0.1 seconds
. R(5,2)
is a range 0.1-0.2 seconds
etc.
范围概念也可以应用于列吗?
Can the range idea also be applied to the columns?
这样做的目的是让我可以将已经组织成一定范围的单元格数组数据读取到零矩阵中,以生成二维直方图.
The purpose for this is so I can read cell array data I have already organised into ranges into the zeros matrix to produce a 2d histogram.
推荐答案
假定您有时间tt
和数据值val
,其中val(i)
包含时间tt(i)
的数据值.在您的示例中,您将拥有
Assume you have the times tt
and the datavalues val
, where val(i)
contains the datavalue for time tt(i)
. In your example you would have
tt = [0.02, 0.22, 0.15, 0.08, 0.27, 0.09];
val = [0.5, 1.4, 2.5, 0.6 , 0.8, 0.3 ];
例如,现在您需要代表所需(增加)的时间和数据范围的向量
Now you need vectors that represent the time and data ranges that you want (increasing), for example
trange = [0, 0.1, 0.2, 0.3, Inf];
valrange = [0, 1, 2, 3, Inf];
现在,您创建尺寸合适的矩阵
Now you create a matrix of the right size
R = zeros(length(valrange), length(trange));
您只需遍历所有时间即可轻松填充矩阵
You can fill the matrix up easily just by looping over all times you have
for i=1:length(tt)
%// We consider the pair tt(i), val(i)
%// First find out, in which time range tt(i) lies:
tind = find(trange > tt(i), 1, 'first');
%// Now find out, in which value range val(i) lies:
valind = find(valrange > val(i), 1, 'first');
%// Now we increase the corresponding matrix entry
R(valind,tind) = R(valind,tind) + 1;
end
请注意,第一列对应于-Inf
至trange(1)
之间的时间范围,最后一列对应于trange(end-1)
至trange(end)==Inf
之间的时间范围.第一行和最后一行的模拟.
Note that the first column corresponds to the time range between -Inf
to trange(1)
and the last column to the range between trange(end-1)
and trange(end)==Inf
. Simliary for the first and last row.
这篇关于MATLAB矩阵范围分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!