How do you circshift progressive rows an increasing amount?

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.

4 件のコメント

David Hyatt
David Hyatt 2020 年 11 月 23 日
編集済み: David Hyatt 2020 年 11 月 24 日
I got this to work:
done = []
for M = 1:5
stemp = circshift(A(:,M),-M,1);
done = [done stemp];
end
------------
Originally I didn't have semicolons. Oops.
Rik
Rik 2020 年 11 月 23 日
Yes, that is indeed very slow. See my answer for a faster solution.
David Hyatt
David Hyatt 2020 年 11 月 24 日
It was going slow because of missing semicolons. Oops. I'm not sure how to turn your solution in to code to try. If you would like to provide a sample I will try it out. The code I made does the 400x400 matrix in about a second for reference.
Rik
Rik 2020 年 11 月 24 日
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.

サインインしてコメントする。

 採用された回答

Rik
Rik 2020 年 11 月 23 日
編集済み: Rik 2020 年 11 月 24 日

0 投票

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);
end
If you want to move every NaN to the bottom, you can also use the sort function.

1 件のコメント

David Hyatt
David Hyatt 2020 年 11 月 25 日
Thank you for the help and explanation!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

リリース

R2020b

質問済み:

2020 年 11 月 23 日

コメント済み:

2020 年 11 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by