フィルターのクリア

how to use circshiftfor each column of a matrix

10 ビュー (過去 30 日間)
ss
ss 2012 年 12 月 22 日
コメント済み: Image Analyst 2022 年 1 月 24 日
Hi I have a matrix that I want to use circshift for its columns, i.e shift the arrays of for instance the first column, do you know how to use circshift?for example I want to convert A=[1 1;2 2;3 3] to b=[3 1;2 2;1 3] thanks anyone
  1 件のコメント
Matt J
Matt J 2012 年 12 月 22 日
There is something wrong with your example. The columns of b and A do not differ by circulant shifts.

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

回答 (4 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 12 月 22 日
out=[flipud(A(:,1)) A(:,2)]

Matt J
Matt J 2012 年 12 月 22 日
編集済み: Matt J 2012 年 12 月 22 日
If each column is to have a different shift, just use a for-loop over the columns of A and apply circshift to each one.

Image Analyst
Image Analyst 2012 年 12 月 22 日
You can use this:
rowsToShift = 1;
b = circshift(A, [rowsToShift 0])
to shift all columns by the same number of rows. But like Matt said, your example is not just a shift - you flipped the first column. Do you want to flip one or more columns? Please clarify if you want to flip certain columns, and specify what flexibility you want, for example you have any number of rows, and any number of columns, and you have a list of which columns you want flipped, or if you just always have a 3 row by 2 column array.

Roger Stafford
Roger Stafford 2012 年 12 月 23 日
If you want a solution without for-loops, try this. Let A be your matrix and S be a row vector of various positive or negative integers giving the desired circular shift amounts in the corresponding columns of A. A and S must accordingly have the same number of columns.
[m,n] = size(A);
[I,J] = ndgrid(0:m-1,0:n-1);
B = A(mod(bsxfun(@minus,I,S),m)+1+m*J);
Roger Stafford
  2 件のコメント
Mingchen Liu
Mingchen Liu 2022 年 1 月 24 日
great! It works well for me! Just wonder that if this will be more efficienct than for-loops?
Image Analyst
Image Analyst 2022 年 1 月 24 日
@Mingchen Liu perhaps, or perhaps not. Just try it and see. Sometimes for loops are faster than built-in functions particularly with small numbers of elements where a for loop can be very fast and sometimes built-in functions have a lot of verification, validation, setup code that can eat up time.

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

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by