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 comentarios

Azzi Abdelmalek
Azzi Abdelmalek el 13 de En. de 2014
If M={1 [7 8] 3 [ 1 5 6] 0 [1 5 8] 2 10 }
What is the expected result?
Benjamin Cowen
Benjamin Cowen el 13 de En. de 2014
I am not sure that I follow. If the cell in row 1, column is 5,6,7 I would like 5 to go in a new matrix in row 1 column 1, 6 to go in row 2, column 2 etc
Azzi Abdelmalek
Azzi Abdelmalek el 13 de En. de 2014
You can simply post the expected result, if really you know what you want
Benjamin Cowen
Benjamin Cowen el 13 de En. de 2014
I really do not know what you mean. The expected result is as follows. I have 106 columns and 1 row of cells. So there are 106 cells. Instead of some cells containing more than 1 number. So if A1 contains numbers 1,2,3 I would like to convert to matrix such that A1 = 1, A2 = 2, A3 = 3. But I have 106 of these and they are different lengths
Benjamin Cowen
Benjamin Cowen el 13 de En. de 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 el 13 de En. de 2014
When I try A(1,j)=cat(1,C{1,j}), I get "Subscripted assignment dimension mismatch."

Iniciar sesión para comentar.

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 13 de En. de 2014

0 votos

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] [] []

Más respuestas (1)

Matt J
Matt J el 13 de En. de 2014

1 voto

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 comentarios

Benjamin Cowen
Benjamin Cowen el 13 de En. de 2014
No. I want 5 to be in column 1, 6,7,8 to be in column 2. I just want the numbers to not be in the same cell. So the first column would have 1 row, the 2nd column would have 3
Matt J
Matt J el 13 de En. de 2014
C={5,[6 7 8], [9,10]};
M=max(cellfun('length',C));
result = cell2mat(cellfun(@(c) pad(c,M), C, 'uni',0)),
function c=pad(c,M)
c=c(:);
c(M+1,1)=0;
c(end)=[];
end
Benjamin Cowen
Benjamin Cowen el 13 de En. de 2014
I get: function definitions are not permitted in this context
Matt J
Matt J el 13 de En. de 2014
Editada: Matt J el 13 de En. de 2014
Function definitions are not allowed inside script files or at the command line. You must do these operations inside a function file. Or, you must put the pad() function in its own file, somewhere visible to MATLAB.
Benjamin Cowen
Benjamin Cowen el 13 de En. de 2014
So I just copy that whole thing into a function file?
Matt J
Matt J el 13 de En. de 2014
Yes. That is one of your options.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Preguntada:

el 13 de En. de 2014

Comentada:

el 13 de En. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by