Convert cell array to matrix

조회 수: 17 (최근 30일)
Antonio Sereira
Antonio Sereira 2017년 4월 5일
답변: Andrei Bobrov 2017년 6월 14일
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
Akira Agata
Akira Agata 2017년 6월 14일
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?

댓글을 달려면 로그인하십시오.

답변 (1개)

Andrei Bobrov
Andrei Bobrov 2017년 6월 14일
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';

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by