0
votes

I was wondering if someone could help me with MatLab. Is there a way to save these 2 values count_zero and count_value into 2 vectors.

The part of interest in the following code is inside the while loop, the upper part is irrelevant for this question.

For example all values of count_zero should be saved in vector a=[count_zero count_zero ..] and all values of count_value in vector b=[count_value count_value ...].

This is my code, thanks in advance.

              threeminutesofvideo_Youtube;
              h=[0:0.5:179];
              for idx=1:length(h)
              threshold=h(idx);
              m =find(threshold-1<=x & x<=threshold);
              Y(idx)=sum(y(m));
              end

  count_zero=0;
  count_value=0;
  i=1;

while i<length(Y)

if (Y(i)==0)  
    count_zero=count_zero+1;
    i=i+1;
    while Y(i)==0  && i<length(Y)
    count_zero=count_zero+1;
    i=i+1;
    end

    if i<(length(Y))
    count_zero
    count_zero=0;
    end

    if i==(length(Y))  &&   Y(length(Y))~=0 
                        count_value=1;
                        count_value
                        count_value=0;
    elseif   i==(length(Y))  &&   Y(length(Y))==0             
                        count_zero=count_zero + 1;  
                        count_zero
                        count_zero=0; 
    end


else
    count_value=count_value+1;
    i=i+1;
    while Y(i)~=0 && i<length(Y)
    count_value=count_value+1;
    i=i+1;
    end
    if i<(length(Y))
    count_value
    count_value=0;
    end

    if i==(length(Y))  &&   Y(length(Y))~=0 
                        count_value=count_value+1;
                        count_value
                        count_value=0;
    elseif   i==(length(Y))  &&   Y(length(Y))==0             
                        count_zero=1;  
                        count_zero
                        count_zero=0; 
    end


end

end

1
Consider adding an index to them, like this count_zero(i). Also, to be on the safe side, consider pre-allocation for them before the while loop.Divakar
when i try to add index like you said count_zero(i) it show me a mistake : Index exceeds matrix dimensions. Error in off_on_vectorsave (line 48) count_value(i)user3464577

1 Answers

1
votes

As far as I have understood you want to memorize the values in a vector, not to save to file don't you? In this case let us call the vector in which you want to memorize If you know a priori the number of values you want to memorize you can do this

a = NaN*ones(num_of_values,1);
i=1;
while condition
    ...
    a(i) = temp_val;
    i = i+1;
end

If you don't know the number of values a priori:

a=[];
i=1;
while condition
     ...
     a = [a;temp_val];
     i=i+1;
end

I hope to have been helpful