How can I rotate a part of a matrix

Hi there,
I have a 4D images (dimensions [156 192 26 42]). I would like to rotate only the last 20 volumes of my 4D matrix and keep the first as it is.
I tried the following:
rotated_matrix=original_matrix;
rotated_matrix = rot90(original_matrix(:,:,:,23:42));
The rotated_matrix has been rotated but it has dimensions [192 156 26 20]. Basically it contains the volumes that have been rotated.
How can I rotate only the volume in the last 20 places and keep the first 22 as they are?

Réponses (1)

Ameer Hamza
Ameer Hamza le 24 Sep 2020
Try something like this
rotated_matrix = original_matrix(:,:,:,[1:20 42:-1:21]);

6 commentaires

DM
DM le 24 Sep 2020
Doesn't seem to be working..
Ok. You wanted to rotate the last 20 only. There will be a minor modification. Does the following work?
rotated_matrix = original_matrix(:,:,:,[1:22 42:-1:23]);
DM
DM le 24 Sep 2020
Yes only the first 20. The above line of code does it only concanate the rotated_matrix and the original_matrix?
It doesn't perform any rotation right?
Ameer Hamza
Ameer Hamza le 24 Sep 2020
It reverse the order of last 20 elements in the 4th dimension. The other elements remain unchanged.
DM
DM le 24 Sep 2020
I might have put my question wrong. I didn't want to reverse the order of the last 20 elements.
I wanted to replace the last 20 elements in the 4th dimension of the original matrix with the 20 elements of the rotated_matrix.
Origina matrix has dimensions [156 192 26 42] where the rotated matrix has dimensions [156 192 26 20].
What I wanted to do is to replace the last 20 elemets of the original matrix with the 20 elemets of the rotated matrix.
Ok. I get it know. Following will work for this case
original_matrix(:,:,:,end-19:end) = rotated_matrix

Connectez-vous pour commenter.

Catégories

Question posée :

DM
le 24 Sep 2020

Commenté :

le 24 Sep 2020

Community Treasure Hunt

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

Start Hunting!

Translated by