help-octave
[Top][All Lists]

## Re: no way for simple matrix lookup ?

 From: Etienne Grossmann Subject: Re: no way for simple matrix lookup ? Date: Wed, 16 Jun 2004 13:57:19 -0400 User-agent: Mutt/1.4.2.1i

```A = reshape (1:12, 3,4);

b = [2 3 1 2 3];
c = [4 1 2 3 4];

D = A(sub2ind (size (A),b, c))

D = diag (A(b,c))              # Don't do that

On Wed, Jun 16, 2004 at 10:56:17AM -0400, D. Goel wrote:
# Hi
#
# [1] Suppose A is a nxn matrix.  And b and are vectors.  How do i
# return elements of A specified by b and c?
#
# i.e. Want to return a vector D, such that
# D(i) = A(b(i), c(i))
#
# Is the only way to do that a for loop?  Is that inefficient?
#
# This "simple lookup" seems too fundamental that there should be a way
# to do it.  Am I missing something?  Else, should this be a feature
# request?
#
# [2] if (1) above is not possible, is it possible if b = c ?
#
# [3] More generally, the problem i am struggling with is:
#
#  A is a nxmxl matrix.  b and c are vectors.
#  I am looking for a vector D, such that D(i,:)= A(b(i), :, c(i)).
#
# Any way to do that?
#
# [4] A simple generalization of (1) to 3 D's possible?
#
#  D(i)=A(b(i), c(i), d(i)) ?
#
#
# [5] BTW, i might be able to do (2) using the diag function, but is
# there a way to use diag for 3 Dimensions?  or use diag such that it
# acts only on 2 of the indices?  So one can implement (3), which is my
# actual problem?
#
#
# [6] Is there a way to transpose the 2nd and 3rd indices of a 3D
# vector?  transpose() complains that it is implemented only for
# 2D's... I am trying 2.1.57.
#
#
#
# Thanks, please CC if you can..
# DG                                 http://gnufans.net/
# --
#
#
#
# -------------------------------------------------------------
# Octave is freely available under the terms of the GNU GPL.
#
# Octave's home on the web:  http://www.octave.org
# How to fund new projects:  http://www.octave.org/funding.html
# Subscription information:  http://www.octave.org/archive.html
# -------------------------------------------------------------
#

--
Etienne Grossmann ------ http://www.cs.uky.edu/~etienne

-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------

```