problem indexing a matrix from arrays

조회 수: 1 (최근 30일)
Tristan
Tristan 2013년 10월 31일
답변: Azzi Abdelmalek 2013년 10월 31일
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