Why do I get subscript indices error?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hi, I have a code like this,
r2 = randi([2 64]);
[m, n] = ind2sub([4 16],r2);
n = (n-1)*60;
CH1(m:end,n:end) = P2(m:end,n:end);
CH1 and P2 are two matrices. I generated 'r2' randomly and used 'ind2sub' to find the indices of the element r2. So I expected m and n to be numbers, but I get this error:
??? Subscript indices must either be real positive integers or
logicals.
Please help.
채택된 답변
Walter Roberson
2016년 10월 28일
r2 = 2:4;
[m, n] = ind2sub([4 16],r2)
(n-1)*60
댓글 수: 8
Sherwin
2016년 10월 28일
Sorry, I don't get it. Would you please explain it a little?
You have
CH1(m:end,n:end) = P2(m:end,n:end);
so you are using n as a subscript.
You used
n = (n-1)*60;
That will come at as 0 if n was 1 before executing that statement.
When can n come out as one in reaching that statement? If
[m, n] = ind2sub([4 16],r2)
is to have the second output come out as 1 then the value being converted, r2, must have been in the first column of the 4 x 16 matrix. That would be the case of r2 was one of 1, 2, 3, or 4. Is that possible? Let us look to see where r2 came from:
r2 = randi([2 64]);
Well that cannot be 1, but it can be 2, 3, or 4. Let us double check that by setting the values to 2, 3, or 4 specifically and trying:
r2 = 2:4;
[m, n] = ind2sub([4 16],r2)
(n-1)*60
and indeed the 3 outputs of (n-1)*60 are 0, and 0 is not a valid subscript.
The bug is either in your bounds for randi, or in your manipulation n = (n-1)*60
Perhaps you wanted
n = (n-1) * 60 + 1;
Sherwin
2016년 10월 28일
Thanks a million!!
In the rest of the code I used a similar method,
r3 = randi([1 N],[G 1]);
for i = 1:G
ch = ceil(r3(i,1)/3840);
[r, c] = ind2sub([4 960],r3-((ch-1)*3840));
if Ma{ch,1}(r,c) == 0
Ma{ch,1}(r,c) = 1;
else
Ma{ch,1}(r,c) = 0;
end
end
This time 'r' and 'c' can't be zero, but I get the same error here! Would you please help me with this too?
Jan
2016년 10월 28일
@Sherwin: The spam flag has been set by accident, most likely. Or perhaps "thanks a million" is too near to "win a million" ;-) I've removed the flag.
Sherwin
2016년 10월 28일
Thank you so much :)
You should use the debugger
dbstop if error
and run. When it stops, tell us what the values of r and c and r3(i) are
Walter Roberson
2016년 10월 29일
You asked this later question at http://www.mathworks.com/matlabcentral/answers/309643-why-do-i-get-an-index-error and it was answered there.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
태그
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
