Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

11 Program for different ways of arrays creation In MATLAB.

12 Program for different types of matrix and array operations In MATLAB.

13 Evaluate Multiple Conditions in Expression.

14 Expressions that include relational operators on arrays, such as A > 0,


are true only when every element in the result is nonzero.

15 Create a function for finding the maximum in a vector.

16 Explain different types of Loops in MATLAB.

17 Write a program for switch cases in MATLAB.

18 Write a program for converting Fahrenheit to Celsius.

19 Write a program for factorial calculation in MATLAB.

20 Write a program for displaying bar plot in MATLAB.


11. Program for different ways of arrays creation In MATLAB.
a = [1 2 3 4]

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

0.8415 0.1411 -0.9589


0.9093 -0.7568 -0.2794
0.6570 0.9894 -0.5440

To transpose a matrix, use a single quote ('):


a'

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

13. Evaluate Multiple Conditions in Expression.

x = 10;
minVal = 2;
maxVal = 6;

if (x >= minVal) && (x <= maxVal)


disp('Value within specified range.')
elseif (x > maxVal)
disp('Value exceeds maximum value.')
else
disp('Value is below minimum value.')
end

Ouput:

Value exceeds maximum value.

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

if any(A > limit)


disp('There is at least one value above the limit.')
else
disp('All values are below the limit.')
end

Output:
There is at least one value above the limit.

15. Create a function for finding the maximum in a vector.


function out = maximum(A)
max(A)
end

Run this in script.

maximum([4,56,67,5])
Output: 67

16. Explain different types of Loops in MATLAB.


For loop in MATLAB.

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

17. Write a program for switch cases in MATLAB.


Display different text conditionally, depending on a value entered at the command prompt.
n = input('Enter a number: ');

switch n
case -1
disp('negative one')
case 0
disp('zero')
case 1
disp('positive one')
otherwise
disp('other value')
end
Output:

At the command prompt, enter the number 1.


positive one

18. Write a program for converting Fahrenheit to Celsius.


fahrenheit=input('Enter deg in F\n');

celsius=5*(fahrenheit-32)/9;

fprintf('%d Fahrenheit is %d Celsius\n',fahrenheit,celsius)

Output
Enter deg in F
34
34 Fahrenheit is 1.111111e+00 Celsius

19. Write a program for factorial calculation in MATLAB.


n = 10;
f = n;
while n > 1
n = n-1;
f = f*n;
end
disp(['n! = ' num2str(f)])

Output:
n! = 3628800

20. Write a program for displaying bar plot in MATLAB.

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 –

You might also like