Cell two mat Execution

3 views (last 30 days)
Jab
Jab on 26 Jan 2016
Commented: Walter Roberson on 26 Jan 2016
A cell array with size 1*178 inside each cell column vector of different sizes. I have to convert this to the matrix with 178 columns and the corresponding column vectors.
Any help is apppreciated! Thanks!

Accepted Answer

Walter Roberson
Walter Roberson on 26 Jan 2016
That is not possible. Numeric matrices cannot have different column lengths for different columns.
  5 Comments
Jab
Jab on 26 Jan 2016
Edited: Walter Roberson on 26 Jan 2016
for k=1,3,4,5,8...etc
% m and m1 are 3D volumes
extracted3{r}=m(m1==3);
r=r+1;
end
For each k, i am getting different vectors in a cell. Is there any way I could concatenate the vectors rather than cell2mat.
Thanks
Walter Roberson
Walter Roberson on 26 Jan 2016
Extracting data that way would make adjacent values in extracted3{r} potentially positionally unrelated to each other -- for example the bottom of one row is in memory near the top of the next row so adjacent values could represent top and bottom. Even without that, the N'th element of one extracted3{r} is probably not going to be related to the N'th element of any other extracted3{r} (if the shape is regular for each k value then the vectors would all end up the same length and we would not be having this discussion.) (Also your sample code is not extracting anything dependent on k, which indicates your sample code is not the real code.)
NaiveBayes is like the other classifiers: the N'th value for any one sample must reflect the same origin as the N'th value of any other sample. For example, the first column being only petal width measurements and the second column being only stamen length measurements. Every sample has to have the same set of measurements in the same order. As that is obviously not the case for your situation, NaiveBayes is not appropriate for your purposes.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 26 Jan 2016
Here's one way that's fairly intuitive and easy to follow.
% Create a cell array, ca, that is random sample data.
for k = 1 : 178
% Get a random length
randomLength = randi(1000, 1);
% Make a column vector of random length.
thisData = rand( randomLength, 1);
% Put this randomly sized data into the cell.
ca{k} = thisData;
end
% =========== MAIN CODE =============================
% Examine the cell array to find the longest vector.
% I'm sure there's a more compact and cryptic cellfun() way,
% but here's an easy-to-follow "for loop" way to find the tallest column vector.
for k = 1 : length(ca);
height(k) = length(ca{k});
end
maxHeight = max(height)
% Now make a 2D numerical array that we can stuff the vectors into
array2d = zeros(maxHeight, length(ca)); % Preallocate.
for k = 1 : length(ca);
thisColumnsHeight = length(ca{k});
array2d(1:thisColumnsHeight, k) = ca{k};
end
% array2d is the output. The bottom rows of any columns will be 0 if
% that column didn't have maxHeight elements in it.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!