MATLAB Answers


Why do i keep getting x=1 when it should be a matrix??

Asked by Nav
on 5 May 2013

For some reason when i run this script x always equals one when it's suppose to be a matrix.

The program is for star dectection from the image and x suppose to be the threshold with steps.

GREY = rgb2gray(stars);
big = max( max (GREY)); %255
threshold = .4;
bwimage = GREY >= threshold*big;
for x = [0.0:step:1.0]
    BW = GREY > big.*x;
    count = sum(BW>=x);

Thanks ill greatly appreciate the help


Log in to comment.

2 Answers

Answer by Walter Roberson
on 5 May 2013
 Accepted Answer

"x" is used as a loop control variable. During the loop it will have only one value at a time; after the loop it will be the last value that it was assigned in the loop.

You have a problem that each iteration of the loop, you are overwriting "BW" and "count".

xvals = 0 : step: 1;
numx = length(xvals);
for K = 1 : numx
  x = xvals(K);
  BW = GREY > big.*x;
  count(K) = sum(BW(:));


Log in to comment.

Answer by the cyclist
on 5 May 2013

The way you have coded this, x is in a for loop, and the code will step through values of x, one by one, starting from 0 and ending at 1.

Instead, did you mean to simply make x a vector? Then you just need

x = 0.0 : step : 1.0;

without the loop.


Log in to comment.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!