How do I fix my array indices? Error says must be positive or logic... (2024)

Walter Roberson on 27 Feb 2023

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1920055-how-do-i-fix-my-array-indices-error-says-must-be-positive-or-logical-values#answer_1181225

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/1920055-how-do-i-fix-my-array-indices-error-says-must-be-positive-or-logical-values#answer_1181225

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

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

  • Link

    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

  • Link

    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

  • Link

    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

  • Link

    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

  • Link

    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

  • Link

    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

  • Link

    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

  • Link

    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.

How do I fix my array indices? Error says must be positive or logic... (2024)
Top Articles
CNN mourns the loss of commentator Alice Stewart - Poynter
Search & Find jobs in Finland • Jobs Portal
Katmoie
Unitedhealthcare Hwp
Byrn Funeral Home Mayfield Kentucky Obituaries
Ati Capstone Orientation Video Quiz
Doublelist Paducah Ky
When is streaming illegal? What you need to know about pirated content
Videos De Mexicanas Calientes
Farmers Branch Isd Calendar
Best Cheap Action Camera
Cosentyx® 75 mg Injektionslösung in einer Fertigspritze - PatientenInfo-Service
Kostenlose Games: Die besten Free to play Spiele 2024 - Update mit einem legendären Shooter
Santa Clara Valley Medical Center Medical Records
C Spire Express Pay
Espn Horse Racing Results
Find Such That The Following Matrix Is Singular.
使用 RHEL 8 时的注意事项 | Red Hat Product Documentation
Lazarillo De Tormes Summary and Study Guide | SuperSummary
Uktulut Pier Ritual Site
U Arizona Phonebook
Copart Atlanta South Ga
Everything you need to know about Costco Travel (and why I love it) - The Points Guy
Quadcitiesdaily
Apartments / Housing For Rent near Lake Placid, FL - craigslist
Bidrl.com Visalia
FAQ's - KidCheck
Creed 3 Showtimes Near Island 16 Cinema De Lux
Bfri Forum
Storelink Afs
Haunted Mansion Showtimes Near Cinemark Tinseltown Usa And Imax
Poster & 1600 Autocollants créatifs | Activité facile et ludique | Poppik Stickers
Ma Scratch Tickets Codes
B.k. Miller Chitterlings
Elizaveta Viktorovna Bout
Craigslist Tulsa Ok Farm And Garden
Academy Sports New Bern Nc Coupons
More News, Rumors and Opinions Tuesday PM 7-9-2024 — Dinar Recaps
Scarlet Maiden F95Zone
Craigslist Malone New York
Avance Primary Care Morrisville
Rs3 Nature Spirit Quick Guide
Denise Monello Obituary
M&T Bank
Searsport Maine Tide Chart
Meet Robert Oppenheimer, the destroyer of worlds
Secrets Exposed: How to Test for Mold Exposure in Your Blood!
Doelpuntenteller Robert Mühren eindigt op 38: "Afsluiten in stijl toch?"
Sitka Alaska Craigslist
Mmastreams.com
Yoshidakins
Salem witch trials - Hysteria, Accusations, Executions
Latest Posts
Article information

Author: Allyn Kozey

Last Updated:

Views: 5707

Rating: 4.2 / 5 (63 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Allyn Kozey

Birthday: 1993-12-21

Address: Suite 454 40343 Larson Union, Port Melia, TX 16164

Phone: +2456904400762

Job: Investor Administrator

Hobby: Sketching, Puzzles, Pet, Mountaineering, Skydiving, Dowsing, Sports

Introduction: My name is Allyn Kozey, I am a outstanding, colorful, adventurous, encouraging, zealous, tender, helpful person who loves writing and wants to share my knowledge and understanding with you.