Effacer les filtres
Effacer les filtres

I am trying to sum the rows in an array of size 2048x2048 of 'unit8' data type.

1 vue (au cours des 30 derniers jours)
In the code below, I take the image in the file attached at try to determine the space in which light is not frequent. Toward the bottom of the code at E = , the code sees an error. It says:
Index in position 1 is invalid. Array indices must be positive integers or logical
values.
Error in matlab_ex (line 77)
E = sum(raw_image,2);
Yet, when the same exact line is executed at B =, it runs perfectly. Could there be a reason for this?
clear all;
% change to desired path
path = 'C:\Users\steven.manz\Documents\Shaky Experiment 05_04_2020\distance_experiment\';
res_x = 2048; res_y = 2048; % resolution
A = double(raw_image);
B = sum(raw_image,1);
sum = 0;
for i = 1025:2048
if B(1,i) < 50
sum = sum + 1;
% disp(sum)
end
end
C = find(B<50);
D = C(1, [37:96]);
fprintf('The number of columns on the right half plane \n in which light seen is under threshold is %.1f\n\n',sum)
fprintf('Which happens at\n')
fprintf('column: %d\n',D)
sum = 0;
for i = 1:1024
if B(1,i) < 50
sum = sum + 1;
% disp(sum)
end
end
D = C(1, [1:36]);
fprintf('The number of columns on the right half plane \n in which light seen is under threshold is %.1f\n\n',sum)
fprintf('Which happens at\n')
fprintf('column: %d\n',D)
E = sum(raw_image,2);
sum = 0;
for i = 1025:2048
if E(1,i) < 50
sum = sum + 1;
% disp(sum)
end
end
C = find(E<50);
D = C(1, [37:96]);
fprintf('The number of columns on the top half plane \n in which light seen is under threshold is %.1f',sum)
fprintf('Which happens at\n')
fprintf('column: %d\n',D)
% sum = 0;
% for i = 1025:2048
% if B(1,i) < 50
% sum = sum + 1;
% disp(sum)
% end
% end
%
% fprintf('The number of columns on the bottom half plane \n in which light seen is under threshold is %.1f',sum)

Réponse acceptée

Ameer Hamza
Ameer Hamza le 5 Mai 2020
Modifié(e) : Ameer Hamza le 5 Mai 2020
You have defined a variable named 'sum' here
sum = 0; % <====== change its name here and everywhere else
for i = 1025:2048
if B(1,i) < 50
sum = sum + 1;
% disp(sum)
end
end
Therefore, in line
sum(raw_image,2)
MATLAB thinks you are referring to the variable named sum, not the function sum(). Change the name of variable to something else and make the replacement everywhere in the code.
  2 commentaires
Steven Manz
Steven Manz le 6 Mai 2020
That fixed the problem! I wasn't aware that I was overloading the variable.
Ameer Hamza
Ameer Hamza le 6 Mai 2020
I am glad to be of help.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Images dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by