# Shifting columns in matrix Matlab

5 views (last 30 days)
Afluo Raoual on 12 Feb 2021
Edited: Afluo Raoual on 16 Mar 2021
Dear members,
I have a matrix in which its tril and triu are zeros
And I want to shift up its rows
How can I program it in Matlab please?
Afluo Raoual on 12 Feb 2021
@the cyclist The empty elements are replaced all by zero elements

the cyclist on 13 Feb 2021
Edited: the cyclist on 13 Feb 2021
Slightly simpler version of @Nora Khaled's idea. It will also generalize to an (N-1) X N array in a way that I believe makes sense, in case that is important.
[~,c] = size(H);
r = c/2;
M = zeros(r,c);
for i=2:2:c
j = i/2;
M(:,i-1:i) = H(j:j+r-1,i-1:i);
end
You don't really need to define r or j as a separate variable, but I think it is a bit clearer.
Also, if you did need this for large arrays, it is more memory-efficient to preallocate M as I did, rather than building it by continually appending.
Afluo Raoual on 13 Feb 2021
Thank you so much. It works

Nora Khaled on 12 Feb 2021
clear all;
clc
H=[0 0 0 0 0 0 0 0 0 0
0 1 1 1 0 0 0 0 0 0
1 1 1 0 0 1 0 0 0 0
1 0 0 1 1 0 0 1 0 0
1 1 0 0 1 1 1 0 0 1
0 0 1 1 1 0 1 1 1 0
0 0 0 0 0 1 0 0 1 1
0 0 0 0 0 0 1 1 0 0
0 0 0 0 0 0 0 0 1 1];
[~,c]=size(H);
j=0;
M=[];
for i=1:2:c
j=j+1;
M=[M H(j:j+4,i:i+1)];
end
Afluo Raoual on 13 Feb 2021
Thank you so much. It works