Convert cell array to matrix

17 Ansichten (letzte 30 Tage)
Antonio Sereira
Antonio Sereira am 5 Apr. 2017
Beantwortet: Andrei Bobrov am 14 Jun. 2017
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 Kommentar
Akira Agata
Akira Agata am 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?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Andrei Bobrov
Andrei Bobrov am 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';

Kategorien

Mehr zu Descriptive Statistics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by