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.

回答(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 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by