MATLAB Answers

Array element rearrange without using conditional statement?

조회 수: 1(최근 30일)
Rubel Ahmed
Rubel Ahmed 2021년 5월 6일
댓글: Rubel Ahmed 2021년 5월 9일
Hi all,
I ahve a Array say A = [1 2 3 4 5 10 13 16 17 8 20 7 23 34 15];
I want to keep the position of first 5 elemnts of A unchanged. Now if any element of A don't satisfy thie following condition will come after the first 5 elements. following conditional statement are 10≤A≤16, 20≤A≤34. those elements whis are not satisfying the following condition will come before the satisfying array elements.
However, I want to see like this
A = [1 2 3 4 5 17 8 7 10 13 16 15 20 23 34 ]

채택된 답변

the cyclist
the cyclist 2021년 5월 6일
I don't get the same output you specify, but I think I obeyed your rules. Did I make a mistake, or did you?
Regardless, maybe you can use my code as a basis of what you want to do.
A = [1 2 3 4 5 10 13 16 17 8 20 7 23 34 15];
A6 = A(6:end);
satisfy = (A6 >= 10 & A6 <= 16) | (A6 >= 20 & A6 <= 34);
out = [A(1:5) A6(~satisfy) A6(satisfy)]
out = 1×15
1 2 3 4 5 17 8 7 10 13 16 20 23 34 15

추가 답변(0개)

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by