Pad empty spaces in matrix with 0
이전 댓글 표시
I have a matrix of the form:
1 4 6 3 7 3
4 6 2 9
5 5 8 2 5
4 8 1 3 9 4
As I accumulated the rows of the matrix from different sources there is a inconsistency in the length of rows. How do I pad the empty spaces with 0? Please help.
댓글 수: 3
Rik
2019년 5월 12일
Are these rows each in a cell? What is your exact data structure?
Meg Cullen
2019년 5월 12일
편집: Meg Cullen
2019년 5월 12일
Image Analyst
2019년 5월 12일
What are the "empty spaces" - do you want to just pad the right hand side?
채택된 답변
추가 답변 (1개)
madhan ravi
2019년 5월 12일
C={[1 4 6 3 7 3] ;...
[4 6 2 9] ;...
[5 5 8 2 5] ;...
[4 8 1 3 9 4] };
M=max(cellfun('prodofsize',C));
Wanted = cell2mat(cellfun(@(x) [x zeros(1,M-numel(x))],C,'un',0))
댓글 수: 2
Rik
2019년 5월 12일
%doesn't work:
Wanted = cell2mat(pad(C,'right',0));
%does work:
Wanted = double(cell2mat(pad(cellfun(@char,C,'un',0),'right',char(0))));
I have the feeling Matlab is getting a bit less weakly typed than it used to be.
madhan ravi
2019년 5월 12일
Wow ,didn’t know that function so +1 .
카테고리
도움말 센터 및 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!