How to select or group sections of an array without using indexing
1 次查看(过去 30 天)
显示 更早的评论
So i have an array of elements like this:
A = [NaN; NaN; NaN; NaN; 2; 3; 6; 7; NaN; NaN; NaN; NaN; NaN; 4; 6; 8; 8; NaN; NaN; NaN; NaN]
What I need is to collect each group of numbers into new separate arrays like this:
B = [2; 3; 6; 7]
C = [4; 6; 8; 8]
How do i do that without hard-coding with the array index numbers? Thanks
0 个评论
采纳的回答
Stephen23
2020-10-20
>> A = [NaN, NaN, NaN, NaN, 2, 3, 6, 7, NaN, NaN, NaN, NaN, NaN, 4, 6, 8, 8, NaN, NaN, NaN, NaN];
>> X = diff([true,isnan(A),true]);
>> B = find(X<0);
>> E = find(X>0)-1;
>> F = @(b,e) A(b:e);
>> C = arrayfun(F,B,E,'uni',0);
>> C{1}
ans =
2 3 6 7
>> C{2}
ans =
4 6 8 8
2 个评论
Stephen23
2020-10-20
X = diff([true;isnan(A(:));true]); % works for either column or row vector A
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!