Borrar filtros
Borrar filtros

Changing elements of column and row in a matrix

5 visualizaciones (últimos 30 días)
Muniba Shah
Muniba Shah el 11 de Nov. de 2018
Comentada: Muniba Shah el 16 de Nov. de 2018
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 comentario
Stephen23
Stephen23 el 11 de Nov. de 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.

Iniciar sesión para comentar.

Respuestas (1)

dpb
dpb el 11 de Nov. de 2018
Editada: dpb el 13 de Nov. de 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 comentarios
dpb
dpb el 13 de Nov. de 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 el 16 de Nov. de 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 :)

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by