How do you circshift progressive rows an increasing amount?

2 views (last 30 days)
If I have a matrix:
[nan nan nan nan nan]
[ 1a nan nan nan nan]
[ 1b 2b nan nan nan]
[ 1c 2c 3c nan nan]
[ 1d 2d 3d 4d nan]
I want to change it to this:
[ 1a 2b 3c 4d nan]
[ 1b 2c 3d nan nan]
[ 1c 2d nan nan nan]
[ 1d nan nan nan nan]
[nan nan nan nan nan]
I tried:
for K = 1:length(A)
A = circshift(A(:,K),K);
Thank you.
Rik on 24 Nov 2020
I have edited my answer to include the edit I meant. Dynamically growing an array is a bad idea, as mlint is warning you. You should make it a habit of resolving every mlint warning. Either suppress them (right-click and select suppress warning on this line), or change the code.

Sign in to comment.

Accepted Answer

Rik on 23 Nov 2020
Edited: Rik on 24 Nov 2020
You need to index a column in A in your output as well.
for K = 1:size(A,2)
A(:,K) = circshift(A(:,K),K);
If you want to move every NaN to the bottom, you can also use the sort function.

More Answers (0)


Find more on Matrices and Arrays in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!