Question regarding arranging matrix values
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a 22*1 matrix with end values zeros.
I want the last two zeros to occupy the first two rows with the other values occupying the respective places after the change.
Which command can i use?
Please help.
댓글 수: 2
Walter Roberson
2021년 11월 30일
Do I understand correctly that you happen to know that Vector(22) is zero, and that there is at least one other zero in the vector but it is not necessarily currently located at Vector(21) ? And you want to move it from where-ever it is to Vector(21) with all of the other entries moving "down" to occupy the hole ?
If so then what do you want to do if there are multiple zeros? Which one of them do you want to move? The first of them? The last that is not already at Vector(21) ?
답변 (2개)
Chunru
2021년 11월 30일
% a 22*1 matrix with end values zeros.
x = rand(22, 1); x(end-5:end)=0;
x
% the last two zeros to occupy the first two rows with
% the other values occupying the respective places after the change.
x = x([end-1:end 1:end-2])
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!