How do I fix my array indices? Error says must be positive or logical values.
34 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Error:
Array indices must be positive integers or logical values.
Error in lefthalfdonutcode (line 155)
profileSums(thisDistance) = profileSums(thisDistance) + double(grayImage(row, column));
I am very beginner to matlab and I am trying to get my attached script to work.
My code is attempting to count the number of dots (using a left half donut shaped mask) over 12 (or 9 the number is arbitrary) consecutive locations evenly distributed across the image and plot the count of the content as a function of the location.
My issues are the integer values are either not logical, or not positive and my code doesn't like that. Everytime I think I fix it, some other related error pops up. I thought the issue was the mask, then the maxDistance but now i am unsure. What would you suggest to get it working? Thank you.
I'm not an artist but i have attached a drawing of what I am trying to accomplish..
I want to make 12 equal bins that segment across the image in a radial mask shape to count the (in this image) purple dots then plot them as a function of their bin locations in a graph. The count would be normalized to the bin area so that the counts in the different bins can be comparable. once I get the code working to do that i think I can finish it up but i have been stuck for weeks on this part and would appreciate help because to me it is not trivial!! Thanks. -N.
0 comentarios
Respuesta aceptada
Más respuestas (3)
Image Analyst
el 28 de Feb. de 2023
I gave you a program for when the zones were 12 vertical rectangles in your other post. Why do you need annular zones now? I see nothing in the image you posted that would indicate annular zones were what you should use.
And, like I said before, when you post stuff like this you need to post the original image and not just the annotated/marked-up one.
If you do want annular zones, you'd need to specify the center of the circles so the zones can be properly computed.
7 comentarios
Morten Sparre Andersen
el 27 de Feb. de 2023
You need to submit more of your code before anybody can comment on what went wrong.
However a variable termed ThisDistance sounds to me as potentially non-integer. Using either floor, round or ceil could possibly be a solution. See help for floor, ceil and round to select the bes for your purpose
2 comentarios
Morten Sparre Andersen
el 27 de Feb. de 2023
Sorry! I didn't look for the code you included.
It is late in my part of the world. I may take a look on your code tomorrow.
Walter Roberson
el 27 de Feb. de 2023
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 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!