Adding columns to matrix using circshift by auto incrementing

I want to develop a square matrix from a column matrix using circshift. Here is an example code:
z=[0;1;2;3];
x=[z,circshift(z,1),circshift(z,2),circshift(z,3)];
Output: >> x
x =
0 3 2 1
1 0 3 2
2 1 0 3
3 2 1 0
I have to do similar for the order of 1200 instead of 4 so I want it to be automatic. Can some one please suggest how can I use loop of vector in this case?
Thanks

 採用された回答

Mischa Kim
Mischa Kim 2014 年 2 月 4 日
編集済み: Mischa Kim 2014 年 2 月 4 日

0 投票

You coud use a simple loop:
z = [0;1;2;3];
z_mat = zeros(length(z));
z_mat(:,1) = z;
for ii = 2:length(z)
z_mat(:,ii)= circshift(z,ii);
end
Initializing a matrix ( zeros ) is faster than appending vectors to a matrix.

1 件のコメント

saad
saad 2014 年 2 月 4 日
Thankyou very much. I did in same way

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

その他の回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2014 年 2 月 4 日

0 投票

z=[0;1;2;3];
x = [z;z];
n = numel(z);
out = x(bsxfun(@minus,(n+1:2*n)',0:n-1));
saad
saad 2014 年 2 月 4 日

0 投票

Thanks for your answers, I already solved using these commands: for n=1:1199 Xm(:,n+1) = circshift(b,n); end

カテゴリ

ヘルプ センター および File ExchangeSparse Matrices についてさらに検索

質問済み:

2014 年 2 月 4 日

コメント済み:

2014 年 2 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by