Matlab File
Matlab File
Output:
a = 1×4
1 2 3 4
To create a matrix that has multiple rows, separate the rows with semicolons.
a = [1 3 5; 2 4 6; 7 8 10]
Output:
a = 3×3
1 3 5
2 4 6
7 8 10
Another way to create a matrix is to use a function, such as ones, zeros, or rand. For
example, create a 5-by-1 column vector of zeros.
z = zeros(5,1)
Output:
z = 5×1
0
0
0
0
0
12. Program for different types of matrix and array operations In MATLAB.
MATLAB allows you to process all of the values in a matrix using a single arithmetic operator or
function.
a = 3×3
1 3 5
2 4 6
7 8 10
a + 10
Output:
ans = 3×3
11 13 15
12 14 16
17 18 20
sin(a)
Output:
ans = 3×3
Output:
ans = 3×3
1 2 7
3 4 8
5 6 10
Concatenation operation
A = [a,a]
Output:
A = 3×6
1 3 5 1 3 5
2 4 6 2 4 6
7 8 10 7 8 10
x = 10;
minVal = 2;
maxVal = 6;
Ouput:
14. Expressions that include relational operators on arrays, such as A > 0, are true
only when every element in the result is nonzero.
limit = 0.65;
A = rand(10,1)
A = 10×1
0.2760
0.6797
0.6551
0.1626
0.1190
0.4984
0.9597
0.3404
0.5853
0.2238
Output:
There is at least one value above the limit.
maximum([4,56,67,5])
Output: 67
for v = [1 5 8 17]
disp(v)
end
Output:
1
17
While loop
limit = 0.8;
s = 0;
while 1
tmp = rand;
if tmp > limit
break
end
s = s + tmp;
end
disp(s)
Output:
1.0926
switch n
case -1
disp('negative one')
case 0
disp('zero')
case 1
disp('positive one')
otherwise
disp('other value')
end
Output:
celsius=5*(fahrenheit-32)/9;
Output
Enter deg in F
34
34 Fahrenheit is 1.111111e+00 Celsius
Output:
n! = 3628800
x = [1:10];
y = [75, 58, 90, 87, 50, 85, 92, 75, 60, 95];
bar(x,y), xlabel('Student'),ylabel('Score'),
title('First Sem:')
print -deps graph.eps
Output
When you run the file, MATLAB displays the following bar chart –