Question

Matlab ==text only not images== ==correct solution only plz== a) Write a nested for loops to...

Matlab

==text only not images==

==correct solution only plz==

a) Write a nested for loops to test if an input Matrix A is a sparse matrix or not. A sparse matrix is a matrix in which most of the elements are zero. By contrast, if most of the elements are nonzero, then the matrix is considered dense. The number of zero-valued elements divided by the total number of elements is called the sparsity of the matrix, so matrix A will be sparse when its sparsity is greater than 0.5.

b) Search the Matlab documentation for a built-in function which check the sparsity of the matrix and rewrite the code using it. (Do NOT use loops)

c) Compute and show the time required to execute the code in (a) and the code in (b). Which code is faster? What conclusion can be drawn from this result? Hint: Use tic…toc commands to compute the time elapsed.

text only not images

correct solution only plz

0 0
Add a comment Improve this question Transcribed image text
Answer #1

(a) MATLAB CODE:

clc
clear all
close all

tic

A = [1 2 3;0 1 0;1 0 0]; % Matrix A
S = size(A); % Size of matrix A
R = S(1); % Number of rows of matrix A
C = S(2); % Number of coloumns of matrix A

Z = 0; % Initializing zeros count
N = 0; % Initializing non-zeros count

for i = 1:R
for j = 1:C
if A(i,j) == 0
Z = Z+1;
else
N = N+1;
end
end
end
fprintf('Number of zeros present in Matrix-A : %d\n',Z)
fprintf('Number of non-zeros present in Matrix-A : %d\n',N)

T = R*C; % Total number of elements in Matrix-A
Sparsity = Z/T; % Sparsity means number of zero valued elements divide by totla number of values in matrix-A
fprintf('Sparsity of Matrix-A:%d\n',Sparsity)

if Sparsity > 0.5
fprintf('Matrix-A is Sparse Matrix\n')
else
fprintf('Matrix-A is Dense Matrix\n')
end

toc

OUTPUT:

(b) MATLAB CODE:

clc
clear all
close all

tic

A = [1 2 3;0 1 0;1 0 0]; % Matrix-A
S = issparse(A); % Checking Matrix-A is sparse or not
if S == 1
fprintf('Matrix-A is Sparse Matrix\n')
else
fprintf('Matrix-A is Dense Matrix\n')
end

toc

OUTPUT:

(c) If we see the elapsed time of part(a) and part(b), part(b) Elapsed time is very very less than that of part(a) elapsed time.That means code in part(b) is faster

Add a comment
Know the answer?
Add Answer to:
Matlab ==text only not images== ==correct solution only plz== a) Write a nested for loops to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Plz use nested loops , read the question carefully, i need help step by step solution...

    Plz use nested loops , read the question carefully, i need help step by step solution using ( spyder).. Exercise 2 Write a Python program (Lab7-ex2.py) that reads from the user an integer n, displays the triangular number sequence up to the n term (ie. 1,3, 6, 10,. n"term). The triangular of a number n is calculated as "n(n+1)/2" where n is a nonzero positive number. Note that your program should display a suitable error message if n is entered...

  • MATLAB Only MATLAB Only MATLAB Only Indicated in the script below, write a function, called arbpoly3,...

    MATLAB Only MATLAB Only MATLAB Only Indicated in the script below, write a function, called arbpoly3, that accepts two inputs: i) a row vector, called c, containing the coefficients of the polynomial, starting with the coefficient for the lowest degree term, which is the constant term. ii) a row vector, called x, which is the set of real numbers where the polynomial is to be evaluated. The output, y, will be a vector containing the values of the polynomial, evaluated...

  • 1. [12 marks] In the following parts of this question, write a MATLAB code to solve a linear syst...

    1. [12 marks] In the following parts of this question, write a MATLAB code to solve a linear system A b (A is a square nonsingular matrix) using Jacobi and Gauss-Seidel algorithms. Do not use the built-in Matlab functions for solving linear systems (a) Write a Matlab function called Jacobi that consumes a square n x n matrix A, and an n x 1 vector b, and uses the Jacobi technique to solve the system Ax-b, starting with the zero...

  • can u please on matlab, i have the solution on paper. [30pts] Write a robust, efficient...

    can u please on matlab, i have the solution on paper. [30pts] Write a robust, efficient MATLAB script to find the eigenvalues and eigenvectors of a 2 x2 matrix input by the user. You should test out your script using the following matrices. 1::)-::) 3 2 3 1 B. 1 2 C 2 3 A- D- 4 1 2 4 4 8 -3 8 You may not use any special MATLAB tools. Instead, work symbolically and derive general expressions for...

  • SOLVE USING MATLAB ONLY AND SHOW FULL CODE. PLEASE TO SHOW TEXT BOOK SOLUTION. SOLVE PART D ONLY

    SOLVE USING MATLAB ONLY AND SHOW FULL CODE. PLEASE TO SHOW TEXT BOOK SOLUTION. SOLVE PART D ONLY Apply Euler's Method with step sizes h # 0.1 and h 0.01 to the initial value problems in Exercise 1. Plot the approximate solutions and the correct solution on [O, 1], and find the global truncation error at t-1. Is the reduction in error for h -0.01 consistent with the order of Euler's Method? REFERENCE: Apply the Euler's Method with step size...

  • Mountain Paths (Part 1) Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e...

    Mountain Paths (Part 1) in C++ Objectives 2d arrays Store Use Nested Loops Parallel data structures (i.e. parallel arrays … called multiple arrays in the zyBook) Transform data Read from files Write to files structs Code Requirements Start with this code: mtnpathstart.zip Do not modify the function signatures provided. Do not #include or #include Program Flow Read the data into a 2D array Find min and max elevation to correspond to darkest and brightest color, respectively Compute the shade of...

  • Unable to correct errors from my MatLab Script and would like to see a script to...

    Unable to correct errors from my MatLab Script and would like to see a script to compare mine to. Write a MatLab script that simulates a virus spread. This problem needs the following parts, some of which are nested loops, ie Part 3, 4 and 5 are nested in Part 2’s loop (for or while): To build the program first to only deal with infection transmission within the town’s neighbors An array which for every person in the town which...

  • For this project, each part will be in its oun matlab script. You will be uploading a total 3 m f...

    For this project, each part will be in its oun matlab script. You will be uploading a total 3 m files. Be sure to make your variable names descriptive, and add comments regularly to describe what your code is doing and hou your code aligns with the assignment 1 Iterative Methods: Conjugate Gradient In most software applications, row reduction is rarely used to solve a linear system Ar-b instead, an iterative algorithm like the one presented below is used. 1.1...

  • Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in...

    Assignment Predator / Prey Objectives Reading from and writing to text files Implementing mathematical formulas in C++ Implementing classes Using vectors Using command line arguments Modifying previously written code Tasks For this project, you will implement a simulation for predicting the future populations for a group of animals that we’ll call prey and their predators. Given the rate at which prey births exceed natural deaths, the rate of predation, the rate at which predator deaths exceeds births without a food...

  • Use the C programming language to complete #include <stdio.h> #include <stdlib.h> #include <float.h> // Declare Global...

    Use the C programming language to complete #include <stdio.h> #include <stdlib.h> #include <float.h> // Declare Global variables here. void array_stats() { // Insert your solution here. } #include <stdlib.h> #include <time.h> int main() { // Simulate the test setup process. srand( time( NULL ) ); for ( int i = 0; i < 32; i++ ) { val[i] = rand(); } val_count = rand(); val_mean = rand(); val_min = rand(); val_max = rand(); // Call submitted code. array_stats(); // Display...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT