0

I don't know how to explain this better than by giving you an example.

Suppose I have the following array:

a = magic(6)

And then I take a 'slice' of that like this:

a(:,1)

It will print:

35
 3
31
 8
30
 4

Now I want the first number, so I want to write:

a(:,1)(1)

Instead of:

b = a(:,1)
b(1)

Also, is there a way to do something like this (assignment and comparison, i.e. set b, then evaluate against it):

(b = a(:,1))(1)

Ok, here's an update with a function where it isn't trivial to use a(1, 1)

come_on = sprintf('%i, ', magic(3));
come_on(1:end-2)
8, 3, 4, 1, 5, 9, 6, 7, 2

Also, what if I only want the first 4 numbers on magic(3)? It would be better to write

sprintf('%i, ', magic(3)(1:4))(1:end-2)

instead of tens of lines, MHO.

Ale Morales
  • 2,728
  • 4
  • 29
  • 42

1 Answers1

1

You cannot concatenate indexing as foo(1)(2)(3). However, you can index multiple dimensions at once. So in this case, a(1,1) will give you what you want.

abcd
  • 41,765
  • 7
  • 81
  • 98
  • Thanks, yeah in this simplified example a(1,1) gives me what I want, but there are others where that is just no possible. So there is absolutely no way to concatenate (or emulate that) indexing? – Ale Morales Jun 25 '12 at 02:58