Azzera filtri
Azzera filtri

Flip the component of column matrix

4 visualizzazioni (ultimi 30 giorni)
Phong Pham
Phong Pham il 1 Mar 2015
Modificato: Stephen23 il 2 Mar 2015
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.

Risposte (1)

Stephen23
Stephen23 il 1 Mar 2015
Modificato: Stephen23 il 1 Mar 2015
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 Commenti
Phong Pham
Phong Pham il 1 Mar 2015
Modificato: Phong Pham il 1 Mar 2015
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 il 2 Mar 2015
Modificato: Stephen23 il 2 Mar 2015
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:

Accedi per commentare.

Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by