Convert cell array to matrix
15 views (last 30 days)
Show older comments
Hi!
I have a 1x100 cell array A and each cell has a different size, e.g. in cell 1 there are 5 numbers 23,24,66,78,55 and in cell 2 there are 6 numbers 4,10,22,65,44,2. Now I want to convert the cell array into a 2D matrix B with size 100x60. The point is that the numbers in cell 1 corresponds to the jth column of the row 1 in the matrix B. In that way I get zeros in the first row but ones at the 23th,24th,66th,78th and 55th columns of the first row.
How I can do this?
For information: The array A I got from findpeaks, where I used a for loop and scanned a 100x60 matrix row by row and selected all maximas above a threshold.
1 Comment
Akira Agata
on 14 Jun 2017
If my understanding is correct,the matrix B should be 100x100. If so, the following code will generate the matrix B which I believe is what you want.
B = zeros(100,100);
for kk = 1:100
idx = false(100,1);
idx(A{kk}) = true;
B(idx,kk) = 1;
end
Is my understanding correct?
Answers (1)
Andrei Bobrov
on 14 Jun 2017
m = max(cellfun(@max,A));
n = numel(A);
B = zeros(m,n);
B(sub2ind([m,n],[A{:}],repelem(1:numel(A),cellfun(@numel,A)))) = 1;
B = B';
0 Comments
See Also
Categories
Find more on Descriptive Statistics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!