top of page
Create a matrix A with the size of NxM

To create matrix A with the size of NxM can do by

1. Input N or number of rows

2. Input M or number of columns

 

code 

      N=input('N ,row = ');

      M=input('M ,column = ');

      A=rand(N,M);

 

     To entries of the matrix A compose of random values by using function rand.

     But from function it will give random values in float numbers between 0 and 1.

     To give values in between -10 and 10 by calculating.

 

code

      A = (rand(N,M)*20)-10;

 

 

Basic MATLAB Programming

 

In the second week of intelligent system classes. Professor have been assigned to study on the basic MATLAB programming. By the way, then the students do research and understand and code on their own programming.

Entries of the matrix A compose of random values in between -10 and 10

Write the matrix A to a file named “test.txt”

     Write matrix A to text file by using function save.

 

code

      save('test.txt', 'A', '-ascii');

 

 

Read the file name “test.txt” and assign the values to the variable B

     Read file by using function dlmread and assign the values to the variable B

 

code

      B = dlmread('test.txt');

 

 

Find the maximum and minimum values of B

    Find minimum value of B and assign to minB by function min.

    Find maximum value of B and assign to maxB by function max. 

 

code

     minB = min(min(B));

     maxB = max(max(B));

 

 

Round each element of B to the nearest integer

    Round each element of B by function round or function nearest.

    

code

      Nearest_Integer = round(B);

 

Check each element is divisible by X

      Check if each element is divisible by X.

If yes, assign new value to such element as 1.

Otherwise, the new value is set to 0.

    

code     

   for M = 1:M 

       for N = 1:N 

           if mod(Nearest_Integer(N,M),X) == 0

               Divisible(N,M) = 1;

           else

               Divisible(N,M) = 0;

           end

       end

   end

 

Check each value of B and assign the new value of B

     Check each value of B,

If it is below -5, assign the value to be 0.

If it is in between -5 and 5, assign the value to be 1.

If it is above 5, assign the value to be 2.

    

code     

   for M = 1:M 

       for N= 1:N 

           if B(N,M) < -5

               Compare(N,M) = 0;

           else if B(N,M) >= -5 && B(N,M) <= 5

                   Compare(N,M) = 1;

               else if B(N,M) > 5

                       Compare(N,M) = 2;

                       end

                end

            end

         end

     end

 

Change matrix into row vector

     Change each element of matrix B into row vector before ploting by function reshape. 

    

code     

      New_B = reshape(Compare,1,[]);

      Original_B = reshape(B,1,[]);

 

Plot the values of new B w.r.t the original values of B 

    Plot the values of New B and Original B on the same graph.

    

code     

      plot(Original_B,'b*-');     

      grid on;    hold;  
      plot(New_B,'r-o');

      xlabel('Row Vector No.');      
      ylabel('Value');

      legend('Original B','New B');

 

bottom of page