如何将不均匀矩阵合并为一个矩阵? [英] How do I combine uneven matrices into a single matrix?
本文介绍了如何将不均匀矩阵合并为一个矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
dt1 dt2 dt3 dt
1 3 6 10
2 4 1 5
3 6 5 3
4 7 4 1
5 1 2 4
6 2 8
7 8
8
9
10
我想将上述数据合并到一个矩阵中(10 x 4).最大行数为10.我创建了zeros
矩阵.但是,我有一个问题,因为数据没有相同的维度.如何获得以下输出?我应该对数据进行排序并将丢失的值替换为0吗?
I have the above data which I want to combine in one single matrix (10 x 4). The maximum number of rows is 10. I created a zeros
matrix. However, I have a problem since the data doesn't have the same dimensions. How it is possible to get the output as below? Should I sort the data and replace the missing values with 0?
1 1 1 1
2 2 2 0
3 3 0 3
4 4 4 4
5 0 5 5
6 6 6 0
7 7 0 0
8 8 8 0
9 0 0 0
10 0 0 10
推荐答案
Here is the modified version of @gnovice's answer to a previous, similar question
%# group the variables. If you would be generating them in a loop, you could use the loop
%# to group them, i.e. have something like
%# for i=1:n
%# dtCell{i} = "function that generates dt_i"
%# end
dtCell = {dt1,dt2,dt3,dt};
nCells = length(dtCell);
maxVal = max(cellfun(@max,dtCell)); %# this way, I don't have to know vector orientation
%# you could replace the loop with calls to cellfun.
%# While this may make you feel more Matlab-ish, it wouldn't be
%# faster, or more readable
out = zeros(maxVal,nCells);
for iCell = 1:nCells
idx = dtCell{iCell}; %# this assignment is just for clarity
out(idx,iCell) = idx;
end
这篇关于如何将不均匀矩阵合并为一个矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文