필터 지우기
필터 지우기

Flip the component of column matrix

조회 수: 4 (최근 30일)
Phong Pham
Phong Pham 2015년 3월 1일
편집: Stephen23 2015년 3월 2일
I have a column matrix such as
A= [ 12
13
14
..
22
23
..
33
34
..
44
43
..]
I want to flip the component to be like this:
A= [ 22
21
20
..
12
33
32
..
23
44
43
..]
I want to flip the row every 11 rows as I show an example above. The .. is just meaning that It continue so we have to the loop.
Please helps me if you have any idea how to approach it. Thanks.

답변 (1개)

Stephen23
Stephen23 2015년 3월 1일
편집: Stephen23 2015년 3월 1일
Note that using a loop is likely to be quite slow. Instead, as long as the number of rows is a multiple of the number of elements that you wish to flip, then this is easy without any loops using reshape and flipud :
N = 5; % number of elements in each group
A = (1:4*N).'; fake data
B = reshape(A,N,[]);
B = flipud(B);
B = reshape(B,[],1);
If the number of elements of A is not a multiple of the then you will need to pad A to become a multiple. You can rearrange the whole code to fit on one line:
B = reshape(flipud(reshape(A,N,[])),[],1);
  댓글 수: 2
Phong Pham
Phong Pham 2015년 3월 1일
편집: Phong Pham 2015년 3월 1일
Thanks for answering Stephen,
The matrix A has 440 rows x 40 columns and I need to flip every 11 elements as shown in example. I only show 1 column on example above but i would like to do the same for all the rest of columns.
A=(1:4*N) and if N=11 as number of elements in each group, then how can they flip all 880 rows?
what is number 4*N? is it how many times I will flip?
Stephen23
Stephen23 2015년 3월 2일
편집: Stephen23 2015년 3월 2일
As I don't have your exact data matrix, I invented my own named A. Its size is 440*4. You can simply use your own matrix in place of A, and this method will work. X is a vector of row-indices, which tells MATLAB where we want to move the rows to. B is exactly A but with every group of eleven rows flipped vertically: this uses MATLAB's indexing ability.
>> A = 11 + (1:440).' * (1:4);
>> X = reshape(flipud(reshape(1:size(A,1),11,[])),[],1);
>> B = A(X,:);
You might also be interested in the answer to this question:

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by