I keep getting the error "Attempted to access counts(8,1); index must be a positive integer or logical." and I'm not sure why. I've looked through my code and it should work as far as I'm aware
Show older comments
This is my script which aims to count the frequency of data inside specific class widths ready to create a probability density function graph.
% matrix range -0.21 < a < 0.64 range = 0.85
a = xlsread('Data.xlsx','W14:W10013');
b = round(a,2);
counts = zeros(85,1);
ii = 1;
for ii = 1:10000
b(ii,1) = (b(ii,1)*100)+22;
ii=ii+1;
end
i=1;
round(b)
for i = 1:10000
counts(b(i,1),1) = counts(b(i,1),1)+1;
i = i+1;
end
Answers (2)
Better Solution
Why waste your time writing slow buggy code when you can simply use one of the inbuilt functions, such as histc or histcounts ?
For Loops
Note that for loops do not require you to preallocate and increment the loop variable yourself. That for operator completely redefines ii so the preallocated variable is useless, and incrementing the variable is performed by the for loop itself, not by that addition. So while you write this:
ii = 1;
for ii = 1:10000
...
ii = ii + 1;
end
in fact you only need this:
for ii = 1:10000
....
end
This is because the for loop defines the value of ii itself, and increments it on every loop iteration. This is clearly shown in the for documentation:
and also many other pages in the documentation:
The Error Message
As for that error it is very easy: indices can only be positive integer values. You are indexing like this:
counts(b(i,1),1)
where b is defined at the top of your file like this:
b = round(a,2);
So b can contain non-integers, because it has been rounded to two decimal places. Is 1.23 an integer? What happens if you try to use 1.23 as an index? Did you check the value when it fails? Place disp(b(i,1)) on the first line of the loop to print its index values. Then look at the last index value when it fails.
Learning to Code
Your title: "it should work as far as I'm aware" makes it clear that there are a few things that you need to learn about writing code. Code does not care what you think, nor whether you want it to work, nor how you believe it works. Code will work properly when you write it properly. Writing code properly means
- Checking every function and making sure that you use it correctly. Read the documentation.
- Checking every line when you write it to make sure that it does what you need it to do.
- Testing the code with expected and edge cases.
- Using the debugging tools.
- Actually paying attention to what the code does, not what you imagine it does.
- and lots of other rules that you should find out yourself.
As a beginner you should work through these tutorials:
And read this:
Image Analyst
on 20 Nov 2015
0 votes
See the FAQ: http://matlab.wikia.com/wiki/FAQ#How_do_I_fix_the_error_.22Subscript_indices_must_either_be_real_positive_integers_or_logicals..22.3F, but I agree with Stephen: don't write your own histogram code, just use the built in functions for it (of which there are many:hist, histc, histogram, histcounts, imhist, ksdensity, etc.).
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!