In: Computer Science
Commands are to be typed in the Command Window in MATLAB [Those preceding the symbol>> are the commands]
16.3 Matrix operations
>>a=[1 2 3;4 5 6;7 8 9]
>>b=[1 0 0;1 0 1;0 0 1]
Assigning:
>>a=b(2,:)
Transposing:
>>[1;2;3]
>>[1;2;3]'
Incrementing:
>>Tempk=a+273.15
Scaling:
>>L=a*2.5
Adding & subtracting:
>>[1 2 3;4 5 6]-[8 7 6;4 2 0]
Repeating:
>>sqrt(a)
>>sqrt(b)
Operating term by term:
>>[1 3;5 8].*[0 1;4 2]
Creating matrix efficiently
>>m=[364;297];
>>n=[3,6,4;2,9,7];
>>p(1,1)=3;
>>q=[2:2:200]
>>r=[1:3:20]
>>w=[2.5:8.5];
>>w=[2.5:8.5]
Advanced Addressing & Matrix operationso
>>M=[123;456;789]
>>M2=M(2:3,1:3)
>>M3=M(2:3,:)
>>M4=M(:,1:2)
>>c=[A;B]
>>A=[1,2;3,4]
>>B=[56;78]
>>C=[A;B]
>>D=[A,B]
The results of the commands when typed on MATLAB command window are provided below:
Please comment if any doubts:
Matrix operations
a=[1 2 3;4 5 6;7 8 9]
b=[1 0 0;1 0 1;0 0 1]
Output:
%Assigning
a=b(2,:)
Output:
%Transposing
[1;2;3]
[1;2;3]'
Output:
%%%Incrementing:
Tempk=a+273.15
OUTPUT:
%%Scaling:
L=a*2.5
Output:
%Adding & subtracting:
[1 2 3;4 5 6]-[8 7 6;4 2 0]
Output:
%%Repeating:
sqrt(a)
sqrt(b)
Output:
%Operating term by term:
[1 3;5 8].*[0 1;4 2]
[1 3;5 8]*[0 1;4 2]
Output:
%%Creating matrix efficiently
m=[364;297];
n=[3,6,4;2,9,7];
p(1,1)=3;
q=[2:2:200]
r=[1:3:20]
w=[2.5:8.5];
w=[2.5:8.5]
Output:
%%Advanced Addressing & Matrix operationso
M=[123;456;789]
%M2=M(2:3,1:3)%%Error , M is a M*1 array
M3=M(2:3,:)
%%M4=M(:,1:2)%%Error , M is a M*1 array
%%c=[A;B]%%Error A and B defined below this line
A=[1,2;3,4]
B=[56;78]
%%C=[A;B] %%%vertical dimensions mismatch (2x2 vs 2x1)
D=[A,B]
Note: This part contains some errors, those are commented above with reason
Output: