Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 将向量划分成不同的矩阵_Matlab - Fatal编程技术网

Matlab 将向量划分成不同的矩阵

Matlab 将向量划分成不同的矩阵,matlab,Matlab,我有一个两个长向量。向量1包含0,1,2,3,4的值,0表示无动作,1表示动作1,2表示第二个动作,依此类推。每个动作都是720个采样点,这意味着您可以找到720个连续的2个,然后是720个连续的4个。向量2包含对应于每个动作的原始数据。我需要为每个动作(1、2、3和4)创建一个矩阵,其中包含第二个向量的相应数据。例如,矩阵1应具有在动作1的相同索引处发生的所有数据(向量2数据)。有什么帮助吗 Example on small amount of data: Vector 1: 0 0 1 1

我有一个两个长向量。向量1包含0,1,2,3,4的值,0表示无动作,1表示动作1,2表示第二个动作,依此类推。每个动作都是720个采样点,这意味着您可以找到720个连续的2个,然后是720个连续的4个。向量2包含对应于每个动作的原始数据。我需要为每个动作(1、2、3和4)创建一个矩阵,其中包含第二个向量的相应数据。例如,矩阵1应具有在动作1的相同索引处发生的所有数据(向量2数据)。有什么帮助吗

Example on small amount of data:
Vector 1: 0 0 1 1 1 0 0 2 2 2 0 0 1 1 1 0 0 2 2 2 
Vector 2: 6 7 5 6 4 6 5 9 8 7 9 7 0 5 6 4 1 5 8 0
Result:
Matrix 1:
5 6 4 
0 5 6
Matrix 2:
9 8 7 
5 8 0

这里有一种方法。我使用一个单元格数组来存储输出矩阵,对这些变量的名称进行硬编码不是一个好计划

V1=[0 0 1 1 1 0 0 2 2 2 0 0 1 1 1 0 0 2 2 2]
V2=[6 7 5 6 4 6 5 9 8 7 9 7 0 5 6 4 1 5 8 0]

%// Find length of sequences of 1's/2's
len=find(diff(V1(find(diff(V1)~=0,1)+1:end))~=0,1)

I=unique(V1(V1>0)); %// This just finds how many matrices to make, 1 and 2 in this case
C=bsxfun(@eq,V1,I.'); %// The i-th row of C contains 1's where there are i's in V1
%// Now pick out the elements of V2 based on C, and store them in cell arrays
Matrix=arrayfun(@(m) reshape(V2(C(m,:)),len,[]).',I,'uni',0);
%// Note, the reshape converts from a vector to a matrix

%// Display results
Matrix{1}
Matrix{2}

下面是另一个解决方案:

v1 = [0 0 1 1 1 0 0 2 2 2 0 0 1 1 1 0 0 2 2 2];
v2 = [6 7 5 6 4 6 5 9 8 7 9 7 0 5 6 4 1 5 8 0];

m1 = reshape(v2(v1 == 1), 3, [])'
m2 = reshape(v2(v1 == 2), 3, [])'

编辑:David的解决方案更灵活,可能更有效。

因为
向量1
中的组长度有一个规则模式,可以利用它在提出解决方案时将许多事情
向量化。这里有一个这样的实现-

%// Form new vectors out of input vectors for non-zero elements in vec1
vec1n = vec1(vec1~=0)
vec2n = vec2(vec1~=0)

%// Find positions of group shifts and length of groups 
df1 = diff(vec1n)~=0
grp_change = [true df1]
grplen = find(df1,1)

%// Reshape vec2n, so that we end up with N x grplen sized array 
vec2nr = reshape(vec2n,grplen,[]).'  %//'

%// ID/tag each group change based on their unique vector 2 values
[R,C] = sort(vec1n(grp_change))

%// Re-arrange rows of reshaped vector2, s.t. same ID rows are grouped succesively
vec2nrs = vec2nr(C,:)

%// Find extents of each group & use those extents to have final cell array output
grp_extent = diff(find([1 diff(R) 1]))
out = mat2cell(vec2nrs,grp_extent,grplen)
给定输入的示例运行-

>> vec1
vec1 =
     0     0     1     1     1     0     0     2     2     2  ...
             0     0     1     1     1     0     0     2     2     2
>> vec2
vec2 =
     6     7     5     6     4     6     5     9     8     7  ...
             9     7     0     5     6     4     1     5     8     0
>> celldisp(out)
out{1} =
     5     6     4
     0     5     6
out{2} =
     9     8     7
     5     8     0

您如何选择何时在
矩阵1
矩阵2
中创建新行?是否总是有正确数量的元素来执行此操作?所有操作的长度都与示例中所示相同。但实际数据要长得多,所以每组1的长度都是3,而这3正好对应于输出矩阵每行的长度。其他动作也是一样。非常感谢,但它给了我一个错误“已知维度的乘积,720,不可分为元素总数,852”是否有一种方法可以检查数组中连续元素的数量,以确保它是720?请参见编辑,这不是一种很好的方法,我不认为,但这是我能想到的最好的了。工作得很好。。谢谢@David