MATLAB Answers

0

Matrix rows and columns swapping

Asked by Rakesh Belchandan on 11 Nov 2019
Latest activity Commented on by Rakesh Belchandan on 12 Nov 2019
I want to completely change rows and column simultaneously (please see the picture attached)
Is there a function which can do this?
Thanks,
Rakesh

  0 Comments

Sign in to comment.

Products


Release

R2018b

2 Answers

Answer by Walter Roberson
on 11 Nov 2019

There is no function that can do that simultaneously, at least not the first time.
For any given row and column exchange pattern, it is possible to pre-process the pattern so that doing the same exchange for multiple different arrays would take place simultaneously for that one matrix. Something like
temp = preprocess_exchange(rows_to_exchange, columns_to_exchange);
newA = A(temp);
newB = B(temp);
where preprocess_exchange is a function that would need to be written.

  2 Comments

I should correct myself: you can do this with indexing.
>> A = [1 2 3;4 5 6;7 8 9]
A =
1 2 3
4 5 6
7 8 9
>> A([2 1 3],[2 1 3])
ans =
5 4 6
2 1 3
8 7 9
Thanks. That works for me.

Sign in to comment.


Answer by Akira Agata
on 12 Nov 2019

I don't think rows and column can be randomly changed simultaneously.
There should be at least 2 steps, like this:
% Input 3-by-3 matrix
A = reshape(1:9,3,3)';
% Randomize rows and columns
A = A(randperm(3),:);
A = A(:,randperm(3));

  1 Comment

Sign in to comment.