MATLAB cell to matrix

1 Ansicht (letzte 30 Tage)
Benjamin Cowen
Benjamin Cowen am 13 Jan. 2014
Kommentiert: Benjamin Cowen am 13 Jan. 2014
I have 1 row and 106 columns. Each cell contains various numbers. Some cells contain 1 number, some contain 5, etc. The problem is this: If one number is present, it looks great. Some cells have more than one number. I would like for the second number to go in the second row, third number in third row, etc. I am not sure how to do this. I am guessing I need to make a zeros matrix or something but I am not sure
  6 Kommentare
Benjamin Cowen
Benjamin Cowen am 13 Jan. 2014
Oh I see what you mean on your first comment. Instead of having multiple numbers in 1 cell, just 1 number in each cell. If a column has more than one number, just put next numbers in next rows
Benjamin Cowen
Benjamin Cowen am 13 Jan. 2014
When I try A(1,j)=cat(1,C{1,j}), I get "Subscripted assignment dimension mismatch."

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 13 Jan. 2014
You can do this
M={1 [7 8] 3 [ 1 5 6] 0 [1 5 8] 2 10 }
n=cellfun(@numel,M)
m=max(n);
N=cell(m,numel(M))
for k=1:numel(M)
N(1:n(k),k)=num2cell(M{k}')
end
The result
N =
[1] [7] [3] [1] [0] [1] [2] [10]
[] [8] [] [5] [] [5] [] []
[] [] [] [6] [] [8] [] []
  1 Kommentar
Benjamin Cowen
Benjamin Cowen am 13 Jan. 2014
Awesome, works perfectly

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Matt J
Matt J am 13 Jan. 2014
Is this what you want?
>> C={5,[6 7 8], [9,10]};
>>cell2mat(C)
ans =
5 6 7 8 9 10
Or,
>> [C{:}]
ans =
5 6 7 8 9 10
  6 Kommentare
Benjamin Cowen
Benjamin Cowen am 13 Jan. 2014
So I just copy that whole thing into a function file?
Matt J
Matt J am 13 Jan. 2014
Yes. That is one of your options.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by