How to index a for loop with negative subscript indices?

Dear MATLAB-users,
could anyone help me by indexing correct subscript indices in a for loop? Aim of the loop is to summarize several gradient images. Therefore, a for loop should run in integer steps from a given range from "-P" to "+P". When running the script, this error occurs: "Subscript indices must either be real positive integers or logicals." Unfortunately, I have to use the given parameter (P), because it is used at different parts of the main script. In the following a simple code of the described problem is attached.
Could anyone tell me how to calculate the sum of the several gradient images?
Many thanks in advance and best regards!
I = im2double(image.tif);
P=2;
I_sum = zeros(16,64);
for l = -P:P
% SHIFT horizontal (l)
I_temp = circshift(I, [0, l]);
% figure('Name','l-shifted image'); imshow(I_temp);
% SUBTRACT horizontally shifted HR-estimation from initial image
I_Gradient = I - I_temp;
% figure('Name','Gradient image'); imshow(I_Gradient);
% ANTISHIFT horizontal (-l)
I_temp = circshift(I_Gradient, -[0, l]);
% figure('Name','Backshifted Gradient image'); imshow(I_temp);
I_sum_(l) = I_temp;
I_sum = I_sum + I_sum(l);
% figure('Name','I_sum'); imshow(I_sum);
end

 Réponse acceptée

You can't use negative indices in Matlab, but this is not a problem. Look at this example
k=0
for l = -P:P
k=k+1
A(k)=l
end

1 commentaire

Henrik
Henrik le 21 Juil 2016
Modifié(e) : Walter Roberson le 21 Juil 2016
Thank you for your fast answer. Unfortunately, it doesn't solve my problem. I have implemented it like this:
I = im2double(image.tif'));
P=2;
I_sum = zeros(16,64);
k=0;
for l = -P:P
k=k+1;
% SHIFT horizontal (l)
I_temp = circshift(I, [0, l]);
% figure('Name','l-shifted image'); imshow(I_temp);
% SUBTRACT horizontally shifted HR-estimation from initial image
I_Gradient = (I - I_temp);
% figure('Name','Gradient image'); imshow(I_Gradient);
% ANTISHIFT horizontal (-l)
I_temp = circshift(I_Gradient, -[0, l]);
% figure('Name','Backshifted Gradient image'); imshow(I_temp);
I_sum_(k) = I_temp(k);
I_sum = I_sum + I_sum(k)
% figure('Name','I_sum'); imshow(I_sum);
end
But the result is not comprehensible to me. The result of my sum-operation should be an image with highlighted edges. Instead I get a black image as output. Could you or anybody else have a look on my code and check if there is a mistake?

Connectez-vous pour commenter.

Plus de réponses (2)

Use this code pattern:
l_vals = -P:P;
num_l = length(l_vals);
for l_idx = 1 : num_l
l = l_vals(l_idx);
...
I_sum(l_idx) = ...
...
end
That is, prepare a variable with a list of all of the values you want to iterate over, in the order you want to iterate over them. Then iterate an index from 1 to the length of that list. At each step, use the index to pull out the particular value for this iteration, for use in your calculations. When you want to store a value that is associated with that particular value, use the index value as the offset to store it.
Henrik
Henrik le 21 Juil 2016
Thank you very much! Regarding the initial question both answers work!
The problem with getting a wrong summation image had its origin in the following rows: I_sum_(k) = I_temp(k); I_sum = I_sum + I_sum(k);
Replacing them with I_sum(:) = I_sum + I_temp; solves this problem.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by