Storing Data from an IF loop

Hi all
so i run a foor loop to get the number of pixels in a sample of images, I then want to find the the jumps that are larger 400 pixels, and the store the x value where this occurs, any idea?
thank you in advance :)
x=0;
for x=1:1:56;
x=x+1;
jump(x)=whitepixels9(x)-whitepixels9(x-1);
if -400>jump>400 ;
end
end

Réponses (3)

Adam Danz
Adam Danz le 3 Mai 2019
Modifié(e) : Adam Danz le 3 Mai 2019

0 votes

There is no such thing as an 'if loop'. There are while loops and for loops but an 'if' statement is a conditional.
If whitepixels9() can operate on vectors, you do not need to (and should not) use a for-loop.
If you must use a for loop, here are some comments to consider:
% x=0; % Remove. Unnecessary.
jump = nan(1,56);
for x=1:1:56;
%x=x+1; % Remove. The for-loop does this automatically
% The line below assumes that the output is a scalar
jump(x)=whitepixels9(x)-whitepixels9(x-1);
% if -400>jump>400 ; % Remove.
%
% end
end
keepX = find(jump > 400);

2 commentaires

Lizzy tatarek
Lizzy tatarek le 3 Mai 2019
I do this but it shows no jumps larger than 400 even though there definitely are?
Adam Danz
Adam Danz le 3 Mai 2019
I just edited my answer. Try that.

Connectez-vous pour commenter.

KALYAN ACHARJYA
KALYAN ACHARJYA le 3 Mai 2019

0 votes

image1=rgb2gray(imread('test.jpg')); % image1 gray image
[rows colm]=size(image1);
fprintf('The number of pixel in the image= %d ',rows*colm);
result1=sort(image1(:,:),'descend');
max_pixels=result1(1:400);
% Next find the indexes wheere max_pixels any elements equal in image 1
Lizzy tatarek
Lizzy tatarek le 3 Mai 2019

0 votes

for x=2:1:56
jump(x)=whitepixels9(x)-whitepixels9(x-1);
if jump(x)>400
jump(x)=whitepixels9(x);
else
jump(x)=0;
end
end
I ended up doing this which is fit for my use and made it possible to plot it on a graph of the pixel number in each image

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by