DATA_BASICS

few elements from Matlab programming

Course homepage: http://cmp.felk.cvut.cz/cmp/courses/ZSO

clear all; rehash;

% create a vector from 0 to 255
a = [0:255];
% revert the elements
b = a(end:-1:1);
% memory allocation
c = zeros(size(a));
th1 = 75; th2 = 180;
% assign 0 to all c-elements which are smaller that the threshold
c(a<th1) = 0;
c(a>th2) = 255;
% note that this is much faster than using a for-cycle
for i=1:size(a,2),
  if a(i)<th1
	c(i)=0;
  elseif a(i)>th2
	c(i)=255;
  end
end
% without memory pre-allocation it would be even slower
d = [];
for i=1:size(a,2),
  if a(i)<th1
	d = [d,0];
  elseif a(i)>th2
	d = [d,255];
  else
	d = [d,a(i)];
  end
end
% when assigning vectors both need to be of the same size
c(a>=th1 & a<=th2) = a(a>=th1 & a<=th2);
% open a figure and clear the possible content
figure(1); clf;
% plot the vector a and hold the content
plot(a,'-g'), hold on
where = 1:10:size(a,2);
% plot only some elements
plot(where,a(where),'*g')
plot(b,'-r'),
plot(c,'-b'),
% better plotting of discrete data
stem(where,c(where),'b')
grid on
title('title of the figure')
xlabel('x-axis label')
ylabel('y-axis label')