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:
accessing multiple elements of matrix

Subject: accessing multiple elements of matrix

From: alistair templeton

Date: 16 Jul, 2008 22:29:04

Message: 1 of 2

So I have a matrix A.
Elsewhere in my code I generate a vector x and vector y, for
example

x = [1 4 2]
y = [8 2 4]

what I'd like to do is recover the elements
A(1,8)
A(4,2)
A(2,4)

Is there a cleaner way to do this than something along the
lines of:

A(x+(y-1)*size(A,1))

Subject: accessing multiple elements of matrix

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 16 Jul, 2008 22:46:01

Message: 2 of 2

In article <g5lsng$24j$1@fred.mathworks.com>,
Alistair Templeton <bigalt2000@yahoo.com> wrote:
>x = [1 4 2]
>y = [8 2 4]

>what I'd like to do is recover the elements
>A(1,8)
>A(4,2)
>A(2,4)

A(sub2ind(size(A),x,y))
--
  "Product of a myriad various minds and contending tongues, compact of
  obscure and minute association, a language has its own abundant and
  often recondite laws, in the habitual and summary recognition of
  which scholarship consists." -- Walter Pater

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