MATLAB: Creating a vector using if and for statement

ifvector

I wrote a code to extract from a vector to another vector only numbers less than 10 and displayed the output to see the process a=[2; 8; 3; 30; 4; 50; 100; 200; 4; 80; 500] :
function b=clasify_decimels(a)
b=zeros(11,1);
for i=1:size(a,1)
if (a(i)<10)
b=a(i);
disp(b)
end
end
The display is fine, it does what it should do:
>> clasify_decimels(a)
2
8
3
4
4
Yet it does not store the output in vector b that I have defined. Why is that?

Best Answer

b(i)=a(i); %should be b(i) instead of b prevents overwriting
%edit
a=[2; 8; 3; 30; 4; 50; 100; 200; 4; 80; 500] :
b=clasify_decimels(a) %function calling
function b=clasify_decimels(a)
b=zeros(11,1);
for i=1:size(a,1)
if (a(i)<10)
b(i)=a(i);
end
b
end
type whos b %in command windows if the values is stored or see workspace