Convert very large nx1 cell into matrix mx3 matrix
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a large cell of size 190004x1. Each cell consists of a number, a decimal point (.), a minus sign (-) or a blank space.
I want to merge each cell element without a space between them into a single cell, whenever a space occurs I want to move to the cell on the right. Whenever a I have 2 spaces I want to start a new line. Here is an example:
댓글 수: 0
답변 (1개)
Wolfgang
2016년 2월 18일
Hi.
First: find out if all of your cell elements are of class type 'char'. Perhaps you have a mix of 'char' and 'double' / 'integer' elements.
If there are not only 'char' elements, you can fix this with:
% Convert all elements in your cell into type 'char'
% (only if you have mixed class types in your cell )
newCell = cellfun(@num2str, yourCell);
As result (newCell) you get a very long character array (string) of the size [nx1]; Now you can split the transposed character array at the delimiter (blank spaces) and convert it into double values
% split the transposed character array at the delimiter (blank spaces)
splittedStrings = strsplit(newCell',' ');
% convert the strings to double values
numbers = str2double(splittedStrings)
Now, you have long vector of numbers. Bring them into matrix form with the reshape command:
% reshape vector to matrix
yourMatix = reshape(numbers,[],3);
I hope this will help you
댓글 수: 0
참고 항목
카테고리
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!