Flip the component of column matrix
조회 수: 3 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (1개)
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
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 Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!