From cell array to matrix
3 ビュー (過去 30 日間)
古いコメントを表示
Hello! I have cell arrays, help me deal with the loop.
for i = 1:length(Animal)-1
Y=[X{i}];
end
data looks like this % X{1}=[0] X{2}=[0 1.2 1.4 1.6] X{3}=[0] X{4}=[0 1.3 1.5 1.6]....
My task is to create a 4x800 matrix, where if zero is the column [0 0 0 0], and the values will be in the column
0 件のコメント
採用された回答
Andrei Bobrov
2019 年 7 月 25 日
Y = cell2mat(cellfun(@(x)[x(:);zeros(4-numel(x),1)],X,'un',0));
3 件のコメント
Andrei Bobrov
2019 年 7 月 25 日
Variant:
n = cellfun(@(x)numel(x(:)),X);
m = max(n);
k = numel(X);
Y = zeros(m,k);
for ii = 1:k
Y(1:n(ii),ii) = x{ii};
end
その他の回答 (1 件)
Felix Albrecht
2019 年 7 月 25 日
Try preallocating with zeros:
Y = zeros(4,800);
% Assuming that length(Animal)-1 = 800
for i = 1:length(Animal)-1
Y(:,i) = X{i};
end
2 件のコメント
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!