Sorting array with missing numbers

조회 수: 11 (최근 30일)
Soumyatha Gavvala
Soumyatha Gavvala 2016년 8월 1일
댓글: Star Strider 2016년 8월 1일
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!

답변 (2개)

Star Strider
Star Strider 2016년 8월 1일
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
Andrei Bobrov
Andrei Bobrov 2016년 8월 1일
accumarray([A{:}]',[A{:}]',[],@(x){x})
Star Strider
Star Strider 2016년 8월 1일
Thank you, Andrei!

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


per isakson
per isakson 2016년 8월 1일
편집: per isakson 2016년 8월 1일
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
>>

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by