Jump to content

MATLAB/Cookbook/Matrix (array) algebra

From Wikiversity
clc;clear all;close all; %Always begin with these statements
fprintf('This code is divided into 4 sections \n' );
%*fprintf* prints to command window. /n makes new line.
sectionNumber= input('Enter case you wish to run: ');
%*input* prompts user to input an integer (1, 2, 3, ...)
%Define your matrix
A = [1 -1; ...
     -1 1]
%The three dots continues the command. Equivalent to  A [1,-1;-1,1]
%Define your solutions
B = [.1; .2]
switch(sectionNumber)
    %*switch* executes only case you select
    % each case should run as stand-alone code
    case 1
        %Multiplication
        fprintf('What is A*B?\n')
        A*B
    case 2
        %Divison
        fprintf('What is A/B?\n')
        B\A
    case 3        
        %Inverse
        fprintf('What is Inverse AB?\n')
        Solution1 = A\B
        %or
        fprintf('or\n')
        Solution2 = inv(A)*B
        
    case 4
        %Transpose
        fprintf('What is transposed A and B?\n')
        A.'
        B.'
        %or
        fprintf('or\n')
        TransposeA2 = transpose(A)
        TransposeB2 = transpose(B)
end % this ends *case*
fprintf('To run this code again type *matrixAlgebra*\n' ); %(if you so name your file)