Cell two mat Execution
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2016-1-26
That is not possible. Numeric matrices cannot have different column lengths for different columns.
5 个评论
Walter Roberson
2016-1-26
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.
更多回答(1 个)
Image Analyst
2016-1-26
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!