problem indexing a matrix from arrays

1 view (last 30 days)
I want matlab to return all the elements of p2 at rows r columns c, I tried p2(r,c), but that doesn't give me the right answer.
p2 =
0.1500 0.1500
0.4804 0.4804
0.5098 0.5098
0.0498 0.0498
1.1008 1.1008
1.0920 1.0920
2.0219 2.0219
1.1342 1.1342
2.9643 2.9643
3.8808 3.8808
3.8487 3.8487
4.9765 4.9765
8.3789 8.3789
7.7730 7.7730
r =
3
6
7
9
10
12
3
6
7
9
10
12
c =
1
1
1
1
1
1
2
2
2
2
2
2
>> p2(r,c)
ans =
Columns 1 through 5
0.5098 0.5098 0.5098 0.5098 0.5098
1.0920 1.0920 1.0920 1.0920 1.0920
2.0219 2.0219 2.0219 2.0219 2.0219
2.9643 2.9643 2.9643 2.9643 2.9643
3.8808 3.8808 3.8808 3.8808 3.8808
4.9765 4.9765 4.9765 4.9765 4.9765
0.5098 0.5098 0.5098 0.5098 0.5098
1.0920 1.0920 1.0920 1.0920 1.0920
2.0219 2.0219 2.0219 2.0219 2.0219
2.9643 2.9643 2.9643 2.9643 2.9643
3.8808 3.8808 3.8808 3.8808 3.8808
4.9765 4.9765 4.9765 4.9765 4.9765
Columns 6 through 10
0.5098 0.5098 0.5098 0.5098 0.5098
1.0920 1.0920 1.0920 1.0920 1.0920
2.0219 2.0219 2.0219 2.0219 2.0219
2.9643 2.9643 2.9643 2.9643 2.9643
3.8808 3.8808 3.8808 3.8808 3.8808
4.9765 4.9765 4.9765 4.9765 4.9765
0.5098 0.5098 0.5098 0.5098 0.5098
1.0920 1.0920 1.0920 1.0920 1.0920
2.0219 2.0219 2.0219 2.0219 2.0219
2.9643 2.9643 2.9643 2.9643 2.9643
3.8808 3.8808 3.8808 3.8808 3.8808
4.9765 4.9765 4.9765 4.9765 4.9765
Columns 11 through 12
0.5098 0.5098
1.0920 1.0920
2.0219 2.0219
2.9643 2.9643
3.8808 3.8808
4.9765 4.9765
0.5098 0.5098
1.0920 1.0920
2.0219 2.0219
2.9643 2.9643
3.8808 3.8808
4.9765 4.9765
I should be getting this:
0.5098
1.092
2.0219
2.9643
3.8808
4.9765
0.5098
1.092
2.0219
2.9643
3.8808
4.9765

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 31 Oct 2013
p2(sub2ind(size(p2),r,c))

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!