Create a new array everytime with the change of sign in an array
7 次查看(过去 30 天)
显示 更早的评论
I have an array A = [0 1 2 3 2 1 0 -1 -2 -2 -1 0 1 1.6 1.8 2 1 0 .... ], Now I want to create a new array everytime the sign changes in the array, it should look like
B = [0 1 2 3 2 1 0]
C = [-1 -2 -2 -1]
D = [0 1 1.6 1.8 2 1 0]
.
.
.
2 个评论
Walter Roberson
2022-4-29
Why should it look like that? sign(0) is not the same as sign(1) . Why are you including 0 in the run of positives? Why does the 0 not get included with the run of negatives?
采纳的回答
Matt J
2022-4-29
编辑:Matt J
2022-4-29
Download these files,
and then do like in the following:
A = [0 1 2 3 2 1 0 -1 -2 -2 -1 0 1 1.6 1.8 2 1 0];
G=groupConsec(A>=0);
output=groupFcn(@(x) {x}, A ,G); %return the groups as cell array elements,
%not separate variables
>> output{:}
ans =
0 1 2 3 2 1 0
ans =
-1 -2 -2 -1
ans =
0 1.0000 1.6000 1.8000 2.0000 1.0000 0
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!