Why do I get subscript indices error?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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?
Walter Roberson
2016 年 10 月 28 日
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 :)
Walter Roberson
2016 年 10 月 28 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
