Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Swap elements in a matrix

Subject: Swap elements in a matrix

From: Jinsoo

Date: 21 Nov, 2012 04:39:14

Message: 1 of 3

Hi,

I have a question about swapping elements in a matrix. I would like to swap elements in the matrix without making a new matrix. Though I googled about any functions, I failed to find out. Below is the example that shows my intent.

a = [ 3 1 2
        2 1 1
        5 4 4 ]

I want to change a(1,2) -> a(2,1) and a(2,1)->a(1,2). The edited matrix a will be like this.

a= [ 3 2 2
       1 1 1
       5 4 4]

Your words will be highly appreciated! Enjoy your Thanksgiving!

Subject: Swap elements in a matrix

From: Nasser M. Abbasi

Date: 21 Nov, 2012 04:55:08

Message: 2 of 3

On 11/20/2012 10:39 PM, Jinsoo wrote:
> Hi,
>
> I have a question about swapping elements in a matrix. I would like to swap elements
>in the matrix without making a new matrix. Though I googled about any functions,
>I failed to find out. Below is the example that shows my intent.
>
> a = [ 3 1 2
> 2 1 1
> 5 4 4 ]
>
> I want to change a(1,2) -> a(2,1) and a(2,1)->a(1,2). The edited matrix a will be like this.
>
> a= [ 3 2 2
> 1 1 1
> 5 4 4]
>
> Your words will be highly appreciated! Enjoy your Thanksgiving!
>


--------------------
tmp=a(1,2); a(1,2)=a(2,1); a(2,1)=tmp
-------------------

a =

      3 2 2
      1 1 1
      5 4 4

Subject: Swap elements in a matrix

From: Roger Stafford

Date: 21 Nov, 2012 05:50:19

Message: 3 of 3

"Jinsoo" wrote in message <k8hlti$d9q$1@newscl01ah.mathworks.com>...
> a = [ 3 1 2
> 2 1 1
> 5 4 4 ]
>
> I want to change a(1,2) -> a(2,1) and a(2,1)->a(1,2).
- - - - - - - - -
  You could use linear indices:

 a([2,4]) = a([4,2]);

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us