Cell two mat Execution
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jab
am 26 Jan. 2016
Kommentiert: Walter Roberson
am 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!
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 26 Jan. 2016
That is not possible. Numeric matrices cannot have different column lengths for different columns.
5 Kommentare
Walter Roberson
am 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.
Weitere Antworten (1)
Image Analyst
am 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!