Sorting array with missing numbers
5 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(2 个)
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 个评论
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
>>
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!