Changing elements of column and row in a matrix

5 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2018-11-11
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.

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2018-11-11
编辑:dpb 2018-11-13
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 个评论
dpb
dpb 2018-11-13
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 2018-11-16
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 :)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by