Flip the component of column matrix
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Stephen23
am 1 Mär. 2015
Bearbeitet: Stephen23
am 1 Mär. 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 Kommentare
Stephen23
am 2 Mär. 2015
Bearbeitet: Stephen23
am 2 Mär. 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:
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!