Cell two mat Execution

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

0 votes

That is not possible. Numeric matrices cannot have different column lengths for different columns.

5 Comments

Jab
Jab on 26 Jan 2016
I need to give this input to Naive Baye`s classifier. Will it accept cell array. Thanks
How many classes do you have? Which observations are your features?
No. fitNaiveBayes() only accepts numeric matrices.
"Each row of X corresponds to one observation (also known as an instance or example), and each column corresponds to one variable (also known as a feature)."
If you were able to generate columns of different lengths, that would logically correspond to the case where some observations were missing data for some features. But if that were the case it would be expected that not all of the samples with missing information happened to be at the bottom, so it would be expected that you had some kind of value that signified missing information. And in that case, you should be putting that flag value into all of the slots, not just ending the column vector early because it was convenient to do so.
I suspect that you are trying to use NaiveBayes to fit data that has multiple feature vectors per sample with the feature vectors being different sizes. If that were the case then you would concatenate all of the feature vectors for any one sample into a row, and each row would end up the same size because each feature vector is consistent about its own size.
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
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)

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

Products

Community Treasure Hunt

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

Start Hunting!