Clear Filters
Clear Filters

Changing elements of column and row in a matrix

5 views (last 30 days)
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 Comment
Stephen23
Stephen23 on 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.

Sign in to comment.

Answers (1)

dpb
dpb on 11 Nov 2018
Edited: dpb on 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 Comments
dpb
dpb on 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.
Muniba Shah
Muniba Shah on 16 Nov 2018
Thank u so much DB,
I got the required output by using;
A = [4 90 6 8;3 91 5 7]
B=[A(:,1) flipud(A(:,2:3).') flipud(A(:,4))]
B(1,2:3)=fliplr(B(1,2:3))
B = [ 4 5 6 7 ; 3 90 91 8 ]
Thanks again :)

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!