Welcome To Home of Information

MATLAB function that takes a matrix as argument and then ask from the user

Posted in
No comments
By Anonymous



MATLAB function that takes a matrix as argument and then ask from the user that which operation you want to perform i.e. (+, -, *) then show the result. For example, if user asks to perform addition then program should show sum of all the values of that matrix. MATRIX CAN BE OF ANY DIMENSION.
function Question10()
    Rows = input('Enter Rows for Matrix A: ');                    %getting no. of rows from user
    Columns = input('Enter Columns for Matrix B: ');                    %getting no. of columns from user
   
    Matrix = zeros(Rows, Columns);                         %initializing matrix
   
    for i=1 : Rows                                             %for loop
        for j=1 : Columns
            fprintf('Enter Value in Row %d Column %d = ', i, j);
            Matrix(i, j) = input('');
        End                     %ending loop
    End                         %ending
   
    disp(' ');
    disp('+');
    disp('-');                  %displayig options
    disp('*');
    disp('/');
    option = input('\nWhich Operation Do you want to Perform: ', 's');     %displayig options
   
    Result = Operations(Matrix, option);
    disp(Result);                      %displayig result
end


0 comments:

MATLAB program which computes the mean and standard deviation of the data. Use FOR LOOPs.

Posted in
No comments
By Anonymous



We can generate a set of data with a random values about some
mean value with the command
num=100;
spr=0.1;
data=5.0+spr*sign(1.0-2.0*rand(1,num)).*rand(1,num)
a) What is the last line doing?
b) Write a MATLAB program which computes the mean and standard deviation of the data. Use FOR LOOPs.


Solution:

function Question9()
    num = 100;
    spr = 0.1;
    data = 5.0 + spr * sign(1.0 - 2.0 * rand(1,num)) .* rand(1,num);         %according to as per given
   
    Total=0;
    Size = size(data);
    for i=1 : Size(2)
        Total = Total + data(i);
    end
   
    Mean = Total/num;                      %getting mean
   
    Sum = 0;
    for i=1 : Size(2)
        Sum = Sum + ((data(i) - Mean) ^ 2);
    end
   
    SD = sqrt(Sum / (num-1));
   
    fprintf('Mean of Data: %.2f \n', Total/num);
    fprintf('SD of data %.2f \n', SD);                           %printig output
end

0 comments:

About the Author

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque volutpat volutpat nibh nec posuere. Donec auctor arcut pretium consequat.

Proudly Powered by Blogger.