Index in position 2 must be positive or logical value error?
2 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
dpb
2022 年 6 月 6 日
If I had to guess, I'd say you've probably got negative latitude/longitude values and it just happens to be the latitude one that popus up first.
Try
any(latitude<0,'all')
採用された回答
Voss
2022 年 6 月 6 日
編集済み: Voss
2022 年 6 月 6 日
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)
x2 = round(100*x)
isequal(x1,x2) % not the same
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
But multiplying by 100 first and then rounding to the nearest integer is (?):
x2 == floor(x2) % integer
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
3 件のコメント
Voss
2022 年 6 月 6 日
@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
その他の回答 (1 件)
Jon
2022 年 6 月 6 日
編集済み: Jon
2022 年 6 月 6 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!