Trying to convert cell to array when cells have range of values
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a 1x1000 cell that I am trying to convert to an array so I can easily copy out all the columns and rows of data. Unfortunately, in each cell there are doubles that vary in length (80x1 double, 81x1 double, 79x1 double) I tried cell2mat but since they are all different lengths that did not work.
What if any function may do what I am trying to do and fill in the void spaces with placeholders that are not 0?
Thank you in advance.
댓글 수: 0
채택된 답변
Rik
2018년 5월 11일
This should do the trick. It assumes array indexing is possible.
a={[1 2 3]',[1 1]',[1 2 3 4 5 6]'};
maxlen=max(cellfun('length',a));
clc
a2=cellfun(@extend_to_maxlength,...
a,repmat({maxlen},size(a)),...
'UniformOutput',false);
a2=cell2mat(a2);
function x=extend_to_maxlength(x,maxlen)
if numel(x)<maxlen
x(maxlen)=0;
end
end
댓글 수: 0
추가 답변 (1개)
Ameer Hamza
2018년 5월 11일
편집: Ameer Hamza
2018년 5월 11일
try this:
% creating test data
a{1} = 1:4;
a{2} = 1:5;
a{3} = 1:10;
% solution
maxElements = max(cellfun(@length, a)); % finding maximum length of appropriate padding of zeros
matrix = cellfun(@(x) [x(:);zeros(maxElements-length(x), 1)], a, 'UniformOutput', 0);
matrix = cell2mat(matrix);
댓글 수: 1
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!