Lecture 4

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

IT Workshop

Programming in MATLAB
Lecture-4: Some Common Functions
size()

sz = size(A) returns a row vector whose elements are the


lengths of the corresponding dimensions of A.
For example, if A is a 3-by-4 matrix, then size(A) returns the
vector [3 4].
>> A=randi(15,5)
A=
13 2 3 3 10
14 5 15 7 1
2 9 15 14 13
14 15 8 12 15
10 15 13 15 11
>> size(A)
ans =
5 5
reshape()

B = reshape(A,sz) reshapes A using the size vector, sz, to define size(B).


For example, reshape(A,[2,3]) reshapes A into a 2-by-3 matrix. sz must
contain at least 2 elements, and prod(sz) must be the same as numel(A).

Reshape Vector into Matrix

Reshape a 1-by-10 vector into a 5-by-2 matrix. B=


A = 1:10;
1 6
B = reshape(A,[5,2]) 2 7
3 8
4 9
5 10
fliplr()
B = fliplr(A) returns A with its columns flipped in the left-right direction (that is, about a
vertical axis).
If A is a row vector, then fliplr(A) returns a vector of the same length with the order of its
elements reversed. If A is a column vector, then fliplr(A) simply returns A. For
multidimensional arrays, fliplr operates on the planes formed by the first and second
dimensions.

Flip a row vector

A = 1:10
A=
1 2 3 4 5 6 7 8 9 10
B = fliplr(A)
B=
10 9 8 7 6 5 4 3 2 1
Create a random logical matrix

rand()

X = rand(n) returns an n-by-n matrix of random numbers

rand(5)>0.5
ans =
0 1 1 1 0
0 0 1 0 0
0 0 1 1 0
1 0 0 0 0
0 0 1 0 0
Create a random binary vector

rand(1,10)>0.5

ans =
0 1 1 0 0 0 1 0 0 1
Convert vector array into matrix

vec2mat()
mat = vec2mat(vec,matcol) converts the vector vec into a matrix with
matcol columns, creating one row at a time. If the length of vec is not a
multiple of matcol, then extra zeros are placed in the last row of mat.

c=rand(1,10)>0.5
c=
0 1 0 0 0 1 1 1 0 1
d=vec2mat(c,2)
d=
0 1
0 0
0 1
1 1
0 1
input()

X=input(prompt,'s’)
It returns the entered text, without evaluating the input as an expression..

s=input('Enter your name','s')


Enter your name
de2bi()
b = de2bi(d) converts a nonnegative decimal integer d to a binary row vector.
If d is a vector, the output b is a matrix in which each row is the binary form of the
corresponding element in d

Convert decimals 1 through 10 into their equivalent binary representations.


ans = 10×5
d = (1:10)';
1 1 0 0 0
b = dec2bi(d);
2 0 1 0 0
[d b]
3 1 1 0 0
4 0 0 1 0
5 1 0 1 0
bi2de() Converts binary values 6 0 1 1 0
to corresponding decimal 7 1 1 1 0
values 8 0 0 0 1
9 1 0 0 1
10 0 1 0 1
Print the ASCII value of your name

s=input('Enter your name','s')


Enter your name sandip

D=double(s)

D=
115 97 110 100 105 112

char(D)

ans =

'sandip'
Assignment-2

Write a program, which takes an input from user and


convert each letter into binary.
Use the following method to encode the user input and
again convert the encoded data into character.

Encoding method:
Replace 2nd column with [1st column xor 3rd column]
Replace 4th Column with [2nd column xor 5th column]
Replace 6th Column with [4th column xor 7th column]
All other columns remain the same

You might also like