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:
Matrix indexing with two arrays question

Subject: Matrix indexing with two arrays question

From: Evan

Date: 12 Jul, 2010 19:59:21

Message: 1 of 3

Say I have a matrix

 Test = [1,2,3,4;
            5,6,7,8];

And two arrays corresponding to the rows and columns of points I'd like to set to zero in the matrix Test.

Rows = [1,2];
Cols = [3, 1];

Is there any way to use a single line of code (I'm trying to avoid loops) to set the positions (1,3) and (2,1) in the matrix Test to 0?

Something like Test(Row, Cols) = 0; that doesn't change four values in Test? Is it possible to limit the indexing into Cols to be just once per Row? Seems like I must be missing something simple...

Thanks!

Subject: Matrix indexing with two arrays question

From: Evan

Date: 12 Jul, 2010 20:13:21

Message: 2 of 3

Nevermind, seems I should use sub2ind. Can't seem to find a way to delete this messgae...

Subject: Matrix indexing with two arrays question

From: us

Date: 12 Jul, 2010 20:52:05

Message: 3 of 3

"Evan " <Evan@fake.com> wrote in message <i1fs6p$2l8$1@fred.mathworks.com>...
> Say I have a matrix
>
> Test = [1,2,3,4;
> 5,6,7,8];
>
> And two arrays corresponding to the rows and columns of points I'd like to set to zero in the matrix Test.
>
> Rows = [1,2];
> Cols = [3, 1];
>
> Is there any way to use a single line of code (I'm trying to avoid loops) to set the positions (1,3) and (2,1) in the matrix Test to 0?
>
> Something like Test(Row, Cols) = 0; that doesn't change four values in Test? Is it possible to limit the indexing into Cols to be just once per Row? Seems like I must be missing something simple...
>
> Thanks!

a hint:

     help sub2ind;

us

Tags for 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