%% 
% amsc/cmsc 460  MATLAB exercise 1

clear all;
close all;
fprintf('\n\n amsc/cmsc 460  matlab exercise 1\n')
%% input
%dimesion
N=3; M=4; 


%% code
%% define A (NxM) and b (Mx1)
A=zeros(N,M);
b=zeros(M,1);

A=[1 1 1 1; 2 2 2 2; 3 3 3 3];

%% column vectors acm & row vectors arn
fprintf('\n\n column vector of A\n')
for m=1:M;
    acm=A(:,m);
    fprintf('\n    m=%d\n',m);
    acm
end
acm=input('  any number to proceed : ')

% row vectors acm
fprintf('\n\n row vector of A\n')
for n=1:N;
    arn=A(n,:);
    fprintf('\n    n=%d\n',n);
    arn
end
arn=input('   any number to proceed : ')

%% practice
nA=sqrt(3)*linspace(1,3,N)';   mA=sqrt(2)*linspace(0,3,M);
mb=[1 4 5 2]';

A=sin(pi*nA)*cos(pi*mA);
b=cos(mb);

% ckeck
c=A*b;
c_ele=mult_mat_vec_ele(A,b);
c_col=mult_mat_vec_col(A,b);
c_row=mult_mat_vec_row(A,b);
dc_ele=max(max(c-c_ele));
dc_col=max(max(c-c_col));
dc_row=max(max(c-c_row));
fprintf('\n  max (c-c_ele) = %f',dc_ele)
fprintf('\n  max (c-c_row) = %f',dc_row)
fprintf('\n  max (c-c_col) = %f\n',dc_col)

%% 
% plot
cM='brkmy';
cN='brkmy';

% row vector
figure('Name','row vectors of A & b')
% row vectors:  arn=A(n,:)
for n=1:N;
    plot([1:M],A(n,:),[cN(n) '-']);
    if(ishold~=1); hold on; end 
end
plot([1:M]',b,cM(N+1),'Linewidth',2);
legend('A(1,:)','A(2,:)','A(3,:)','b')
for n=1:N;
    plot([1:M],A(n,:),[cN(n) '.']);
end
title('row vectors of A & b','fontsize',12)
xlabel('m','fontsize',12); ylabel('a^r_n','fontsize',12);

% column vectors:  acm=A(:,m)
figure('Name','column vectors of A')
for m=1:M;
    plot(A(:,m),[1:N]',[cM(m) '-']);
    if(ishold~=1); hold on; end
end
legend('A(:,1)','A(:,2)','A(:,3)','A(:,4)')
for m=1:M;
    plot(A(:,m),[1:N]',[cM(m) '.']);
end
title('column vectors of A','fontsize',12)
ylabel('n','fontsize',12); xlabel('a^c_m','fontsize',12);
axis([-1 1 1 N])
    


