%% SEEING A VECTOR AS AN IMAGE
%  This code allows the user to view a vectorized image (which you
%  probably created with LoadPics).  A vectorized image is a
%  color image that has been reshaped into a vector.  The user
%  should know the original size of the image.

%% Syntax (assuming V is the result of ResizeMatrix, or similar)
% MyImage = SeeVector(V)

%% The function is called SeeVector
function I = SeeVector(V);

%% To use & edit this code, paste the following into MATLAB, hit return and save the file as SeeVector.m
% grabcode('http://euclid.nmu.edu/~joshthom/Teaching/MA211/SeeVector.html');

%% Get size of the expected input vector or ask for help if the size is unexpected.
	[r,c] = size(V);
	yn    = 'y';  			

	if min(r,c) ~= 1; 
		disp('Give me a vector'); 
		return; 
	end

	% The expected size is 30 x 40 (see ResizeMatrix.m)	
	if max(r,c) == 30*40*3;
		R=30;
		C=40;
  	else
		yn = input('Is your image a color image? [y/n] ', 's');
    	R = str2num(input('How many pixels WIDE is your image? ', 's'));
    	C = str2num(input('How many pixels TALL is your image? ', 's'));
  	end

	if (R*C ~= r*c) && (R*C*3 ~= r*c);
		 disp('The dimensions you entered do not match the image');
		 return;
    end

%% Reshape accordingly
	if ~isempty(yn) && strcmp(yn,'y')
	   	n = length(V)/3;
	   	RGB{1} = V(1:n);
	   	RGB{2} = V(1+n:2*n);
	   	RGB{3} = V(2*n+1:3*n);
  	 	
		for i = 1:3
   			I(:,:,i) = uint8(reshape(RGB{i},R,C));
   		end
	else
		I = uint8(reshape(V,R,C));
	end

%% The resized image
	image(I);
	hold on;
	axis off;
