Walter Roberson on 27 Feb 2023
Open in MATLAB Online
thisDistance = round(sqrt((row-y)^2 + (column-x)^2));
Your x and y are 300 x 300 arrays, so (row-y) returns an array and then (row-y)^2 is matrix power -- (row-power) matrix multiplied by (row-power) . Which is permitted because it happens to be a square matrix, but probably you would want .^ instead.
The result in thisDistance would be an 2d array of distances
if thisDistance <= 0
continue;
end
so you are checking whether the 2D array of distances is <= 0 . If x and y contain only real values, then sqrt((row-y)^2 + (column-x)^2) would be non-negative -- possibly 0 but never negative -- so <= 0 would effectively be testing for == 0 (since < 0 cannot occur for reals in this situation.) Well, at least not once you repair the ^2 to be .^2 .
When you if a non-scalar, the test is considered true only if all of the values being tested are non-zero. So in order for the test to be considered true, all of the values in thisDistance would have to be non-positive, and if there is even a single positive distance then the test would be false and you would not continue
profileSums(thisDistance) = profileSums(thisDistance) + double(grayImage(row, column));
and there you are using the 2D array as indices -- but because of the mistake in the if test, you failed to reject the case where at least one value was 0 but not all of the values were 0. So you potentially have 0 as a subscript, which is not permitted.
Be further warned: the values in thisDistance will have numerous duplicates. So there will, for example, probably be about 12 places that are distance 2 away, so your code is like (for example) profileSums([3 2 2 2 3; 2 1 1 1 2]) = profileSums([3 2 2 2 3; 2 1 1 1 2]) + double(grayImage(row,column)) . This is at the very least going to cause you confusion. Should the value be added to index 2 once for each different place the distance was computed as 2? Or should the value be added to index 2 once on if at least one place computed the distance as 2? If the answer is "once for each time" then you are calculating that incorrectly. If you want "once only" then you should unique(thisDistance) to find the indices that should be incremented.
8 Comments Show 6 older commentsHide 6 older comments
Show 6 older commentsHide 6 older comments
Neo on 28 Feb 2023
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/1920055-how-do-i-fix-my-array-indices-error-says-must-be-positive-or-logical-values#comment_2637130
@Walter Roberson
So what would be the correction? I tried what you suggested and the code is not working as I intended which is to "to count the number of dots (with a left half donut shaped mask) over 9 consecutive
% locations evenly distributed across the image and plot the count as a function of the location.'"
It helps if i can see how you get it to work and fix my mistake that way (beginner problems). I am a wet sponge. Thanks.
Image Analyst on 28 Feb 2023
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/1920055-how-do-i-fix-my-array-indices-error-says-must-be-positive-or-logical-values#comment_2638455
Open in MATLAB Online
@Neo Your code is very confusing. You're trying to do it two different ways at the same time. I suggest you make a flow chart and pick one of the ways. You need to make up your defining radii
% Get the radial distance to the edges of each zone.
numZones = 12;
zoneRadii = linspace(0, maxDistance, numZones + 1)
and then decide if you want to make 12 annular zone masks and then process the whole zone at once, like
profile(zoneNumber) = nnz(maskImage(zoneMask));
Or if you want to scan the image pixel by pixel determining what zone that pixel belongs to and incrementing the count for that zone.
profile(zoneNumber) = profile(zoneNumber) + 1;
Walter Roberson on 28 Feb 2023
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/1920055-how-do-i-fix-my-array-indices-error-says-must-be-positive-or-logical-values#comment_2638565
Open in MATLAB Online
Counting the number of dots does not involve adding the content of the image
double(grayImage(row, column))
If you have an array of distances thisDistance and want to count each of the distances, then
counts_by_distance = accumarray(thisDistance(:)+1, 1);
Neo on 1 Mar 2023
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/1920055-how-do-i-fix-my-array-indices-error-says-must-be-positive-or-logical-values#comment_2639015
@Image Analyst
When I put your line in the code it doesn't work it might work if you put it in then attach it so I can see it next to mine. I made the flow chart in my above comment but basically its like your radial profile program but with 12 equal bins and for multiple images at once. Oh and it would count the number of spots in the image not the average gray level.
Neo on 1 Mar 2023
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/1920055-how-do-i-fix-my-array-indices-error-says-must-be-positive-or-logical-values#comment_2639020
@Walter Roberson
When I put the referenced line of code to make it work it doesn't work as expected maybe you can include it in the script then reattach it so I can compare it?
Image Analyst on 1 Mar 2023
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/1920055-how-do-i-fix-my-array-indices-error-says-must-be-positive-or-logical-values#comment_2639140
Of course those lines alone won't work. You have to have a whole program. Those snippets are just the "meat" of the program. I gave you two options for program flows. One to create 12 masks, and one to scan pixel by pixel. Both methods will work. Which way do you want to do it?
Neo on 1 Mar 2023
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/1920055-how-do-i-fix-my-array-indices-error-says-must-be-positive-or-logical-values#comment_2639170
Edited: Neo on 1 Mar 2023
The way where you create 12 angular masks.
Neo on 2 Mar 2023
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/1920055-how-do-i-fix-my-array-indices-error-says-must-be-positive-or-logical-values#comment_2641025
Where in my code do I start to go two different ways? I’m going to have a good look tomorrow and see if I can fix it based on your feedback so far but if you can give me how you would fix it to check against afterwards would be awesome, thanks
Sign in to comment.