Sorting array with missing numbers

6 visualizaciones (últimos 30 días)
Soumyatha Gavvala
Soumyatha Gavvala el 1 de Ag. de 2016
Comentada: Star Strider el 1 de Ag. de 2016
Hello,
I have a cell array that goes like this:
A= [ 1
2
3
7
8
10
14
15]
I want to insert empty data or null data where there are no numbers, like this
A=[1
2
3
_
_
_
7
8
_
10
_
_
_
14
15]
How can I do this?
Thank you!

Respuestas (2)

Star Strider
Star Strider el 1 de Ag. de 2016
The accumarray function can do this
A= {1
2
3
7
8
10
14
15};
ix = cumsum(diff([0 [A{:}]]));
Afill = accumarray(ix', [A{:}], [], @(x){x})
Afill =
[ 1]
[ 2]
[ 3]
[]
[]
[]
[ 7]
[ 8]
[]
[10]
[]
[]
[]
[14]
[15]
  2 comentarios
Andrei Bobrov
Andrei Bobrov el 1 de Ag. de 2016
accumarray([A{:}]',[A{:}]',[],@(x){x})
Star Strider
Star Strider el 1 de Ag. de 2016
Thank you, Andrei!

Iniciar sesión para comentar.


per isakson
per isakson el 1 de Ag. de 2016
Editada: per isakson el 1 de Ag. de 2016
Is this what you are looking for?
A = [1;2;3;7;8;10;14;15];
B = nan( A(end), 1 );
B(A) = A;
>> B'
ans =
1 2 3 NaN NaN NaN 7 8 NaN 10 NaN NaN NaN 14 15
>>

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by