Why do I get an index error?

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 日

0 投票

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.

1 件のコメント

Sherwin
Sherwin 2016 年 10 月 28 日
Thank you!

サインインしてコメントする。

Guillaume
Guillaume 2016 年 10 月 28 日

0 投票

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 件のコメント

Sherwin
Sherwin 2016 年 10 月 28 日
Thank you for your answer, but r3 is a random column vector with the length of G and each time one element is picked to enter this term:
r3-((ch-1)*384
and the random element is between 1 and N so the smallest amount r3 can take is 1, and if this happens,
ch = ceil(r3(i,1)/3840)= ceil(1/3849) = 1
[r, c] = ind2sub([4 960],1-((1-1)*3840)) =
[r,c] = ind2sub([4 960], 1) = 1
this should happen.There shouldn't be negative results.
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 日

0 投票

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!

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeChemistry についてさらに検索

タグ

質問済み:

2016 年 10 月 28 日

編集済み:

2016 年 10 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by