# Index in position 2 must be positive or logical value error?

3 views (last 30 days)
Ali Almakhmari on 6 Jun 2022
Commented: Voss on 6 Jun 2022
I am about to pull my hair out. Why is this giving me an error. I checked my math about nine times and its fine. Either I am insane or MATLAB is giving me the wrong error (I attached the "picture" variable and the "longitude" and "latitude" as .mat files in the google drive link cause they are more than 5 MB). I appreciate any help. https://drive.google.com/drive/folders/1Nw50uOfz_fHVQtmk0awbdKeGn4okiM-X?usp=sharing
FULL_PICTURE = zeros(36001, 18001, 3);
for i = 1:length(latitude(:,1))
for j = 1:length(latitude(1,:))
I = 100*(round(longitude(i,j),2)+180) +1;
F = 100*(round(latitude(i,j),2)+90) +1;
FULL_PICTURE(I, F, :) = picture(i,j,:);
end
end
##### 2 CommentsShowHide 1 older comment
Ali Almakhmari on 6 Jun 2022
I do have negative latitudes but the equation that calculates the index always ends up positive because my minmum value of latitude is -58 ish.

Voss on 6 Jun 2022
Edited: Voss on 6 Jun 2022
You'd think that 100*round(x,2) would be an exact integer value and in fact would be the same as round(100*x), but that's not necessarily true:
format long
x = 77.2071507771809848463817615993320941925; % latitude(1,15), more-or-less
x1 = 100*round(x,2)
x1 =
7.720999999999999e+03
x2 = round(100*x)
x2 =
7721
isequal(x1,x2) % not the same
ans = logical
0
In particular, rounding to 2 decimal places and then multiplying by 100 is not guarateed to give you an integer:
x1 == floor(x1) % not an integer
ans = logical
0
But multiplying by 100 first and then rounding to the nearest integer is (?):
x2 == floor(x2) % integer
ans = logical
1
So when you get a not-exactly-an-integer number and try to use in as an index, you get that error.
Instead try your code like this, where round is done after the multiplication:
FULL_PICTURE = zeros(36001, 18001, 3);
for i = 1:length(latitude(:,1))
for j = 1:length(latitude(1,:))
I = round(100*longitude(i,j)) + 18001;
F = round(100*latitude(i,j)) + 9001;
FULL_PICTURE(I, F, :) = picture(i,j,:);
end
end
Voss on 6 Jun 2022
@Ali Almakhmari: By the way, a useful approach to solving this type of problem - where the code fails sometimes and you don't see any reason why - is to issue the command
dbstop if error
and then run the code. The code will stop executing when an error happens and give you a chance to inspect the values of the variables, so you can see what causes the error.
An alternative that I tend to use (for no reason other than that's what I'm used to doing) is to wrap the erroneous line in a try/catch with a keyboard in the catch part. That is, something like this:
FULL_PICTURE = zeros(36001, 18001, 3);
for i = 1:length(latitude(:,1))
for j = 1:length(latitude(1,:))
I = round(100*longitude(i,j)) + 18001;
F = round(100*latitude(i,j)) + 9001;
try
FULL_PICTURE(I, F, :) = picture(i,j,:);
catch
% when the error happens, you'll get control on the
% command line, so you can see what's going on
keyboard
end
end
end

### More Answers (1)

Jon on 6 Jun 2022
Edited: Jon on 6 Jun 2022
I think the problem is that you are rounding to 2 decimal place rather than just rounding to an integer. This makes indices for I and F that are not integers
So replace
I = 100*(round(longitude(i,j),2)+180) +1;
F = 100*(round(latitude(i,j),2)+90) +1;
with
I = 100*(round(longitude(i,j))+180) +1;
F = 100*(round(latitude(i,j))+90) +1;
##### 1 CommentShowHide None
Jon on 6 Jun 2022
@Voss approach (multiply by 100 first then round) is better if you need to maintain the extra two digits of precision in your integer value

R2022a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by