Question regarding arranging matrix values

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 Comments

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) ?
Only vector 21 and 22 are zero.
I want to shift them to the first and second rows.
The existing first, second.....20 vectors should start occupying from 3rd place to 22nd place.
Eg:Consider the matrix given below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
0
0
I want to convert it as
0
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

Sign in to comment.

Answers (2)

% a 22*1 matrix with end values zeros.
x = rand(22, 1); x(end-5:end)=0;
x
x = 22×1
0.5377 0.2918 0.7060 0.3794 0.5667 0.8483 0.6654 0.0483 0.9393 0.0060
% 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])
x = 22×1
0 0 0.5377 0.2918 0.7060 0.3794 0.5667 0.8483 0.6654 0.0483

Products

Release

R2021b

Asked:

on 30 Nov 2021

Answered:

on 30 Nov 2021

Community Treasure Hunt

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

Start Hunting!