필터 지우기
필터 지우기

Why do I get an index error?

조회 수: 20 (최근 30일)
Sherwin
Sherwin 2016년 10월 28일
편집: David Goodmanson 2016년 10월 28일
Here in this part of my code
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
'r' and 'c' can't be zero, but I get:
??? Subscript indices must either be real positive integers or
logicals
Would you please help me with this?
  댓글 수: 2
KSSV
KSSV 2016년 10월 28일
[r, c] = ind2sub([4 960],r3-((ch-1)*3840));
The values inside ind2sub should be positive integers. In your case they might be taking zeros and/or negative values. Check that.
Sherwin
Sherwin 2016년 10월 28일
편집: Sherwin 2016년 10월 28일
But I think the smallest amount that this term can get:
r3-((ch-1)*3840
is 1! not negative not 0...

댓글을 달려면 로그인하십시오.

답변 (3개)

Steven Lord
Steven Lord 2016년 10월 28일
On which line does this error occur?
If it's on the line where you define r3, you've probably created a variable named randi that is shadowing the built-in randi function.
For the line where you define ch, you've probably shadowed ceil.
In either of these cases, rename or remove the variable.

Guillaume
Guillaume 2016년 10월 28일
It's not clear what you're trying to do with your code, but clearly it's not going to work
r3 is a vector with values between 1 and an unknown N. So, let's assume that r3(1) is N and r3(2) is 1. Because of the way you create ch it is an integer at least 1, and more if N > 3840. For the error you see, N must be at least 3841, so let's assume that,
So, for i = 1, r3(i) = 3840, and ch = ceil(3841/3840) = 2. You then have
r3 - ((ch-1)*3840) = [3841 1] - (1 * 3840) = [3841 1] - 3840 = [1 -3839]
As you can see you've got a very negative index here. It gets even worse if N > 2*3840.
Note that there is no point in the ind2sub call in your code. You could just as well use the linear index to index Ma{ch} with absolutely no change of behaviour.
  댓글 수: 3
Guillaume
Guillaume 2016년 10월 28일
편집: Guillaume 2016년 10월 28일
I thought my demonstration was clear. Create the following variables:
N = 3841; %minimum value for the error to occur
r3 = [N; 1]; %a possible output of your randi. Don't need any more elements to break your code. G is irrelevant.
i = 1; %first pass of the loop
Now, look at the values of
ch = ceil(r3(i) / 3840) %is ceil(3841/3840) = 2
r3 - ((ch - 1) * 3840
You'll see that the output of the last expression is
[1; -3839]
Anyway, it would be much better if you told us what you're trying to achieve as I'm fairly certain you're going at it completely wrong.
Sherwin
Sherwin 2016년 10월 28일
Thank you so much.. I got it :)

댓글을 달려면 로그인하십시오.


David Goodmanson
David Goodmanson 2016년 10월 28일
편집: David Goodmanson 2016년 10월 28일
Hi Sherwin, May be there shouldn't be negative indices, but there actually are negative indices, as Guillaume has pointed out. Here is an example, running your code:
G = 2;
r3 = [1; 3841];
for i = 1:G
ch = ceil(r3(i,1)/3840);
[r, c] = ind2sub([4 960],r3-((ch-1)*3840))
end
which for i=2 comes up with
c = -959
1
Perhaps you want to have r3(i,1) on the fourth line of your original code so as to make a single index instead of a vector's worth.
  댓글 수: 1
Sherwin
Sherwin 2016년 10월 28일
Thank you so much!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by