如何将非均匀矩阵合并为单个矩阵?

发布于 2024-09-08 07:18:45 字数 451 浏览 4 评论 0原文

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 吗?

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
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        

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慕巷 2024-09-15 07:18:45

这是 @gnovice 对之前类似问题的回答

%# 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

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
儭儭莪哋寶赑 2024-09-15 07:18:45

让我们定义示例数据:

dt1 = randi(10,10,1)-1;
dt2 = randi(10,7,1)-1;
dt3 = randi(10,6,1)-1;
dt4 = randi(10,5,1)-1; %// example data. Column vectors.

一种方法是使用 bsxfun,然后在掩码指示的位置填充值:

dt = {dt1, dt2, dt3, dt4}; %// collect data into a cell array
n = cellfun(@numel, dt); %// length of each vector
mask = bsxfun(@le, (1:max(n)).', n); %// create mask
result = zeros(size(mask)); %// initiallize result with zeros
result(mask) = vertcat(dt{:}); %// fill in values.

示例结果(带有随机数据):

result =
     2     3     2     7
     3     9     8     6
     4     7     9     0
     4     3     5     7
     2     1     1     1
     1     7     7     0
     8     0     0     0
     1     0     0     0
     7     0     0     0
     8     0     0     0

Let's define example data:

dt1 = randi(10,10,1)-1;
dt2 = randi(10,7,1)-1;
dt3 = randi(10,6,1)-1;
dt4 = randi(10,5,1)-1; %// example data. Column vectors.

One approach is to create a mask with bsxfun and then fill values at the positions indicated by the mask:

dt = {dt1, dt2, dt3, dt4}; %// collect data into a cell array
n = cellfun(@numel, dt); %// length of each vector
mask = bsxfun(@le, (1:max(n)).', n); %// create mask
result = zeros(size(mask)); %// initiallize result with zeros
result(mask) = vertcat(dt{:}); %// fill in values.

Example result (with random data):

result =
     2     3     2     7
     3     9     8     6
     4     7     9     0
     4     3     5     7
     2     1     1     1
     1     7     7     0
     8     0     0     0
     1     0     0     0
     7     0     0     0
     8     0     0     0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文