Using uint8 as matrix index
7 次查看(过去 30 天)
显示 更早的评论
I have a matrix "parent" defined as uint32 type. Later, i use this parent to index another matrix
Counter(parent(2));
But i get the error 'Subscript indices must either be real positive integers or logicals.'
I have defined "Parent" as uint32 to save memory.
0 个评论
采纳的回答
the cyclist
2016-5-31
编辑:the cyclist
2016-5-31
This code works perfectly fine for me:
A = uint32([4 5 6]);
idx = uint8(2);
A(idx)
It also works if idx is defined as a uint32.
Maybe you can post a code snippet that illustrates the error you are getting?
2 个评论
the cyclist
2016-5-31
Shouldn't be a problem. My best guess is that you are accidentally trying to index with a zero, and the memory-saving integer situation is a red herring.
更多回答(2 个)
Image Analyst
2016-5-31
Chances are that the index is zero, or possibly negative.
3 个评论
Walter Roberson
2016-5-31
编辑:Walter Roberson
2016-5-31
Is your code happening to try to store negative values for the counters? Does it arrive at the counters by doing subtractions that might happen to result in negative values?
Put a conditional breakpoint on the line
dbstop at LINENUMBER if parent(2) == 0
and run again and I suspect you will discover that the value is 0.
the cyclist
2016-5-31
Salman, you now have 3 out of the top 10 contributors on this forum guessing at a solution. Can you please distill the simplest possible version of your code that will display this error, and post it? (Sometimes the distillation process itself will expose the error.)
Image Analyst
2016-6-1
Note: parent and Parent are two different things because MATLAB is case sensitive. In your question you use it both ways and if you do that in your code, you will possibly have a problem. Do a case insensitive search for parent and make sure you're consistent in how you're using the letter case (capitalization).
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!