Random Matrix in Matlab

randi() function is used to retrieve an array of integer, valued from give list in order of m*n dimensions used as :
randi([<start range> <ending range>],<number of rows>,<number of columns>)

Example

>> randi([2 6],2,3) % elements 2 to 6 (randomly) in matrix form of 2 rown and 3 columns

ans =

     3     5     6
     5     3     2

>> randi([1 100],3,4) % elements 1 to 100 (randomly) in matrix form of 3 rown and 4 columns

ans =

    67    28    49    55
    89    95    14    84
     5    87     8    19

Writing data to file in Matlab using fprintf

fprintf is used to write data to file.

Example

fileID = fopen('anyFile.txt','w'); % OPEN FILE IN WRITE MODE

x = 0:1:10; % RANGE FROM 1 TO 10
A = [x; x.^2]; % x AND SQUARE OF x

fprintf(fileID,'%6s %12s\n','x','Square of x');
fprintf(fileID,'%6.2f %12.8f\n',A);
fclose(fileID);

Output (anyFile.txt)

     x  Square of x
  0.00   0.00000000
  1.00   1.00000000
  2.00   4.00000000
  3.00   9.00000000
  4.00  16.00000000
  5.00  25.00000000
  6.00  36.00000000
  7.00  49.00000000
  8.00  64.00000000
  9.00  81.00000000
 10.00 100.00000000

REPMAT for repeating array patterns

repmat is used for producing repeating matrix in matlab arrays.

Syntax

repmat( <matrix>, <number of rows> , <number of columns>)

Example

>> repmat([ 1 2 3], 3 , 5)

ans =

     1     2     3     1     2     3     1     2     3     1     2     3     1     2     3
     1     2     3     1     2     3     1     2     3     1     2     3     1     2     3
     1     2     3     1     2     3     1     2     3     1     2     3     1     2     3

>> repmat([ 1 2 3], 3 , 2)

ans =

     1     2     3     1     2     3
     1     2     3     1     2     3
     1     2     3     1     2     3

>> a = [2 3; 6 9]

a =

     2     3
     6     9

>> repmat(a,2,3)

ans =

     2     3     2     3     2     3
     6     9     6     9     6     9
     2     3     2     3     2     3
     6     9     6     9     6     9

Plotting points in Matlab

plot function is used to plot variables in Matlab.

Example

>> y

y =

     6     3     5     9    20    42    21    12    55    22

>> x = [ 1 : 10 ]

x =

     1     2     3     4     5     6     7     8     9    10

>> y = [6 3 5 9 20 42 21 12 55 22]

y =

     6     3     5     9    20    42    21    12    55    22

>> plot(x,y,'.b') % .b for blue dots, you may also remove it, it will work

Output

plot

Extracting video frames from specific index in matlab

When there is not enough memory available for extracting all the video frames in matlab, we often read specific frames by giving the indices instead of reading all of the frames into the memory.

Example

obj = mmreader('abc.avi');
vid = read(obj,[150 300]); % read(obj,[<starting index of frames> <ending index of frames>])
frames = obj.NumberOfFrames;
for x = 1 : frames
    imwrite(vid(:,:,:,x),strcat('frame-',num2str(x),'.jpeg'));
end

plot images in matlab

figure : figure creates figure graphics objects. Figure objects are the individual windows on the screen in which the MATLAB software displays graphical output.

Example

clear all
a = imread('images111.jpg');
aa = rgb2gray(a);
imshow(aa);
figure, bar3(aa,'r') % r for red bars

Output

imnoise (adding noise to images)

imnoise is used to add noise in images.

Syntax

A = imnoise(I,type)
A = imnoise(I,type,parameters)
A = imnoise(I,'gaussian',M,V)
A = imnoise(I,'localvar',V)
A = imnoise(I,'localvar',image_intensity,var)
A = imnoise(I,'poisson')
A = imnoise(I,'salt & pepper',d)
A = imnoise(I,'speckle',v)
gpuarrayA = imnoise(gpuarrayI,___)

Description

A = imnoise(I,type) adds noise of a given type to the intensity image I. type is a string that specifies any of the following types of noise. Note that certain types of noise support additional parameters. See the related syntax.

Value Description
'gaussian' Gaussian white noise with constant mean and variance
'localvar' Zero-mean Gaussian white noise with an intensity-dependent variance
'poisson' Poisson noise
'salt & pepper' On and off pixels
'speckle' Multiplicative noise

Individual Details

A = imnoise(I,'gaussian',M,V): adds Gaussian white noise of mean m and variance v to the image I. The default is zero mean noise with 0.01 variance.
A = imnoise(I,'localvar',V):adds zero-mean, Gaussian white noise of local variance V to the image I. V is an array of the same size as I.
A = imnoise(I,'localvar',image_intensity,var): adds zero-mean, Gaussian noise to an image I, where the local variance of the noise, var, is a function of the image intensity values in I. The image_intensity and var arguments are vectors of the same size, and plot(image_intensity,var) plots the functional relationship between noise variance and image intensity. The image_intensity vector must contain normalized intensity values ranging from 0 to 1.
A = imnoise(I,'poisson'): generates Poisson noise from the data instead of adding artificial noise to the data. If I is double precision, then input pixel values are interpreted as means of Poisson distributions scaled up by 1e12. For example, if an input pixel has the value 5.5e-12, then the corresponding output pixel will be generated from a Poisson distribution with mean of 5.5 and then scaled back down by 1e12. If I is single precision, the scale factor used is 1e6. If I is uint8 or uint16, then input pixel values are used directly without scaling. For example, if a pixel in a uint8 input has the value 10, then the corresponding output pixel will be generated from a Poisson distribution with mean 10.
A = imnoise(I,'salt & pepper',d): adds salt and pepper noise to the image I, where d is the noise density. This affects approximately d*numel(I) pixels. The default for d is 0.05.
A = imnoise(I,'speckle',v): adds multiplicative noise to the image I, using the equation J = I+n*I, where n is uniformly distributed random noise with mean 0 and variance v. The default for v is 0.04.
gpuarrayA = imnoise(gpuarrayI,___) :adds noise to the gpuArray intensity image gpuarrayI, performing the operation on a GPU. Returns a gpuArray image J of the same class.

Example

a = imread('Desert.jpg');
b = imnoise(a,'gaussian',0.01);

subplot(1,2,1); imshow(a); title('a');
subplot(1,2,2); imshow(b); title('b');

Output

double function Matlab

double() function Convert symbolic matrix to MATLAB numeric form.

Syntax

r = double(S)

Description

r = double(S) converts the symbolic object S to a numeric object r.

Input Arguments

s
Symbolic constant, constant expression, or symbolic matrix whose entries are constants or constant expressions.

Output Arguments

r
If S is a symbolic constant or constant expression, r is a double-precision floating-point number representing the value of S. If S is a symbolic matrix whose entries are constants or constant expressions, r is a matrix of double precision floating-point numbers representing the values of the entries of S.

Example

>> img = imread('Desert.jpg'); % reading an image
>> class(img) % determining type of image which is object here
ans =
uint8
>> img1 = double(img); % converting object to double
>> class(img1) % determining type of image which is double now
ans =
double

subplot function in matlab

subplot() function is used to plot 1 or more images on the same common MATLAB panel.

Syntax

subplot(<no of rows>,<no of columns>,<position of element from top left>), <image>;

Example

a = imread('Desert.jpg');
b = imread('desertg.jpg');
c = imread('images.jpg');
d = imread('penguins.jpg');
e = imread('Desertg.jpg');
f = imread('newImage.png');

% MATRIX CONTAINS 2 ROWS AND 3 COLUMNS
% 2x3 IS CONSTANT THROUGHOUT SUBPLOT, ONLY ELEMENT NUMBER CHANGES
subplot(2,3,1), imshow(a); % (first element of 2x3 matrix from top left)
subplot(2,3,2), imshow(b); % (second element of 2x3 matrix from top left)
subplot(2,3,3), imshow(c); % (third element of 2x3 matrix from top left)
subplot(2,3,4), imshow(d); % (fourth element of 2x3 matrix from top left)
subplot(2,3,5), imshow(e); % (fifth element of 2x3 matrix from top left)
subplot(2,3,6), imshow(f); % (sixth element of 2x3 matrix from top left)

Output

Logical AND (&&) operator matlab

The single ampersand & is the logical AND operator. The double ampersand && is again a logical AND operator that employs short-circuiting behaviour. Short-circuiting just means the second operand (right hand side) is evaluated only when the result is not fully determined by the first operand (left hand side).

A & B A and B are evaluated
A && B B is only evaluated if A is true

Example

function y = dummyFunction() 
for i = 1:6
    if i > 3 && i < 5
        disp('less than 5 and greater than 3')
    else
        disp('greater than 5 and less than 3')
    end
end

Output

greater than 5 and less than 3
greater than 5 and less than 3
greater than 5 and less than 3
less than 5 and greater than 3
greater than 5 and less than 3
greater than 5 and less than 3

If else statement matlab

if expression, statements, end evaluates an expression, and executes a group of statements when the expression is true.

Example

for i = 1:20
    if i == 10              % checking if clause
        disp('inside 10')   % printing message inside 10
        break               % breaking out from loop
    else
        disp(i)             % printing i value
    end
end

Output

     1
     2
     3
     4
     5
     6
     7
     8
     9
inside 10

Subtract two images Matlab

The pixel subtraction operator takes two images as input and produces as output a third image whose pixel values are simply those of the first image minus the corresponding pixel values from the second image. It is also often possible to just use a single image as input and subtract a constant value from all the pixels. Some versions of the operator will just output the absolute difference between pixel values, rather than the straightforward signed output.

How It Works :

The subtraction of two images is performed straightforwardly in a single pass. The output pixel values are given by:

Q(i,j) = P1(i,j) – P2(i,j)

Or if the operator computes absolute differences between the two input images then:

Q = |P1(i,j) – P2(i,j)|

Or if it is simply desired to subtract a constant value C from a single image then:

Q = P1(i,j) – C

Example

A = imread('1.jpg');
B = imread('2.jpg');
other1 = imsubtract(B, A); % subtract two images

subplot(1,3,1), imshow(imadd(A,100));
subplot(1,3,2), imshow(imadd(B,100));
subplot(1,3,3), imshow(other1);

Output

Untitled

Blending images using imadd Matlab

Blending Adding images together produces a composite image of both input images. This can be used to produce blending effects using weighted addition.

Example

A = imread('testFirst.jpg');
B = imread('testSecond.jpg');
other1 = imadd(B,A); % added two images

subplot(1,3,1), imshow(imadd(A,100)), title('A'); % added 100 so that image should not look dark
subplot(1,3,2), imshow(imadd(B,100)), title('B'); % added 100 so that image should not look dark
subplot(1,3,3), imshow(other1), title('Final Image'); % displaying final image

Output

Untitled