How to index a for loop with negative subscript indices?
Mostrar comentarios más antiguos
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
Respuesta aceptada
Más respuestas (2)
Walter Roberson
el 21 de Jul. de 2016
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
el 21 de Jul. de 2016
0 votos
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!