COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### index question #2

• Follow

```Hi all

I have a matrix:

A=magic(10);
B=[1 3 7];
C=[2 4 6];

out=A(B, C);

I want to see
out=[A(1, 2) A(3, 4) A(7, 6)]=[99 20 48]

But the result is

out=[
99	8	67
81	20	54
5	89	48
]

How can I do what I want to do?

Thanks!
```
 0
Reply ryuyr77 (123) 3/15/2010 4:45:30 PM

```On Mar 15, 12:45=A0pm, "Young Ryu" <ryuy...@gmail.com> wrote:
> Hi all
>
> I have a matrix:
>
> A=3Dmagic(10);
> B=3D[1 3 7];
> C=3D[2 4 6];
>
> out=3DA(B, C);
>
> I want to see
> out=3D[A(1, 2) A(3, 4) A(7, 6)]=3D[99 20 48]
>
> But the result is
>
> out=3D[
> 99 =A0 =A0 =A08 =A0 =A0 =A0 67
> 81 =A0 =A0 =A020 =A0 =A0 =A054
> 5 =A0 =A0 =A0 89 =A0 =A0 =A048
> ]
>
> How can I do what I want to do?
>
> Thanks!

hey,
post the values of A please.
```
 0
Reply jason.mellone (42) 3/15/2010 4:54:54 PM

```"Young Ryu" <ryuyr77@gmail.com> wrote in message <hnlo7a\$oa7\$1@fred.mathworks.com>...
> Hi all
>
> I have a matrix:
>
> A=magic(10);
> B=[1 3 7];
> C=[2 4 6];
>
> out=A(B, C);
>
> I want to see
> out=[A(1, 2) A(3, 4) A(7, 6)]=[99 20 48]
>
> But the result is
>
> out=[
> 99	8	67
> 81	20	54
> 5	89	48
> ]
>
> How can I do what I want to do?
>
> Thanks!

The reason you get that result is that matlab interprets that syntax to mean "columns 1 3 7" and "rows 2 4 6".  It does not interpret it pairwise the way you want it to.  You need the SUB2IND function to create linear indices into A:

>>A=magic(10);
>>B=[1 3 7];
>>C=[2 4 6];
>>linearIndices = sub2ind([10 10],B,C);
>>out = A(linearIndices);

the cyclist
```
 0
Reply thecyclist (381) 3/15/2010 5:07:09 PM

2 Replies
36 Views