problem indexing a matrix from arrays

1 次查看(过去 30 天)
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

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2013-10-31
p2(sub2ind(size(p2),r,c))

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by