Write a MATLAB program that finds all negative numbers in a

Write a MATLAB program that finds all negative numbers in a matrix and then sets them all to zero. Your program should first ask users to enter the matrix.

Solution

MATLAB CODE:-

clear
clc
r = input(\'no.of rows = \'); % Enter no.of rows of a matrix
c = input(\'no.of columns = \'); % Enter no.of columns of a matrix
disp(\'Enter Matrix Elements\')
for i = 1:1:r
    for j = 1:1:c
        A(i,j) = input(\'\');% Eneter Matrix
        j = j+1;
    end
    i = i+1;
end
for i = 1:1:r
    for j = 1:1:c
        A1 = A(i,j);
        if (A1<0)
            A2(i,j) = 0;
        else
            A2(i,j) = A1;
        end
        j = j+1;
    end
    i = i+1;
end
A2


OUTPUT :-

no.of rows = 3
no.of columns = 3
Enter Matrix Elements
-6
-8
6
9
1
-5
-6
7
3

A2 =

     0     0     6
     9     1     0
     0     7     3

 Write a MATLAB program that finds all negative numbers in a matrix and then sets them all to zero. Your program should first ask users to enter the matrix.Solu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site