Changing elements of column and row in a matrix
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi, how can I change the positions of different elements of rows and columns in a matrix [2x4]? I have matric A = [4 90 6 8;3 91 5 7] and want to change it to B = [4 5 6 7;3 90 91 8] for that i have tried i have tried B=[A(:,1) flipud(A(:,2)) A(:,3) flipud(A(:,4))] but stuck in changing second column of first row to third column of second row. After that, I want to change B into to C = [6 3;7 4;5 8;90 91]
1 commentaire
Stephen23
le 11 Nov 2018
The flip function reverses the order of elements along one dimension. These are your matrices:
>> A = [4 90 6 8;3 91 5 7]
A =
4 90 6 8
3 91 5 7
>> B = [4 5 6 7;3 90 91 8]
B =
4 5 6 7
3 90 91 8
>> C = [6 3;7 4;5 8;90 91]
C =
6 3
7 4
5 8
90 91
None of your matrices are related to each other by flipping along any dimension. So it is not clear how you think flip will help you. Also, the algorithm for rearranging those element is not clear, so it is unlikely that you will get any particularly useful answer until you explain the rearrangement algorithm.
Réponses (1)
dpb
le 11 Nov 2018
Modifié(e) : dpb
le 13 Nov 2018
One way...
>> flipud(A(:,2:3).')
ans =
6 5
90 91
>>
Given the Comment below of arbitrary arrangement being arbitrary and the desire for flip, we strive to please! :)
>> A(:,2:3)=flipud([A(:,2) flipud(A(:,3))].')
A =
4 5 6 8
3 90 91 7
>>
BTW, flipud can be replaced with the new flip in this instance with same result.
3 commentaires
dpb
le 13 Nov 2018
Old eyes fail again... ;(
To get an arbitrary rearrangement requires an arbitrary manipulation of the inputs...or, as Stephen notes, an algorithm that explains the desired reordering.
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!