フィルターのクリア

Changing elements of column and row in a matrix

5 ビュー (過去 30 日間)
Muniba Shah
Muniba Shah 2018 年 11 月 11 日
コメント済み: Muniba Shah 2018 年 11 月 16 日
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 Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by