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

Answers (3)

Adam Danz
Adam Danz on 3 May 2019
Edited: Adam Danz on 3 May 2019
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 Comments

I do this but it shows no jumps larger than 400 even though there definitely are?
I just edited my answer. Try that.

Sign in to comment.

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
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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 3 May 2019

Answered:

on 3 May 2019

Community Treasure Hunt

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

Start Hunting!