combine different size vectors into one matrix
43 次查看(过去 30 天)
显示 更早的评论
I need to combine sets of data I have together into one matrix, but they have different dimensions.
this is a simplified example:
e=[2 3 4 5];
>> ee=[4 7 7];
>> eee=[8 8 7 4 2 5];
>> p=[e; ee; eee];
I want to fill nan to make them even, but I don't know how.
I want each vector to be in a raw, so I get something like that
p=[2 3 4 5 NaN NaN;
4 7 7 NaN NaN NaN;
8 8 7 4 2 5];
then I want to get the mean for each column data
any suggestions please.
0 个评论
采纳的回答
Chunru
2021-9-17
编辑:Chunru
2021-9-17
% use a cell array instead of separate variables
e{1}=[2 3 4 5];
e{2}=[4 7 7];
e{3}=[8 8 7 4 2 5];
% find the longest vector
l = max(cellfun(@(x) length(x), e))
y = nan(length(e), l);
for i=1:length(e)
y(i, 1:length(e{i})) = e{i};
end
y
ym = mean(y, 1, 'omitnan') % mean along 1st dimension or column
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!