Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Solve using MATLAB (not by paper) and using only nested loops. Please provide steps explanations and screensots of the code and output. Loops and Vectorization.

Solve using MATLAB (not by paper) and using only nested loops. Please provide steps explanations and screensots of the code and output. image text in transcribed

image text in transcribed
Loops and Vectorization. In Matlab, the code has been optimized so that most operations involve vectorization which is generally faster than loops. However, loops remain an important part of computing and an approach for which you should have significant expertise. In this exercise we will look at four problems that you have solved previously using Matlab and you will now implement its equivalent using loops. Note - you will have to think carefully to solve these problems - I suggest you try earnestly before consulting others, to test your logic skills. 1a. Jeopardy Previously you have solved the jeopardy problem in which 5 values from 200 to 1000 was repeated into 6 columns. Sample Matlab code was range - linspace (200, 1000,5); mult = ones (1,6); jep = range. *mult You are to replicate this jeopardy matrix jep using only nested loops. ib. Meshgrid Previously meshgrid was introduced as a way to multiply two vectors of arbitrary length allowing for all possibilities in each vector. Sample Matlab code was: a = [1:4]; b = [5:10]; [A, B] =meshgrid(a,b); Prod = A.*B Given the same initial vectors a and b, replicate Prod using only nested loops. ic. Reshape Previously you reshaped a column vector into a rectangular matrix using the reshape command. For example, if we start with a sample vector which is 20 elements long, then we can reshape to a 5 x 4 (or 4 x 5 or 10 x 2 or 2 x 10) using Matlab code such as: Vec = [1:20]; M=5 ; N=4; ReVec = reshape (Vec, [M, N]) Given the same intial vector Vec, replicate ReVec using only nested loops. You code should work for any properly allowed values M and N where M*N=original length of Vec. Also, in this example create an initial vector which is quite long (e.g. 20,000 elements) and use tic and toc in your code to estimate the time used by reshape and the time used by your code. Definitely avoid printouts in this exercise. Which is faster and by how much? id. Matrix Multiplication Matrix Multiplication of an M x P matrix (A) with a P x N matrix (B) yields an M x N matrix (C) with the Matlab command: C=A*B Replicate this result by using three nested loops. Your code should work for any compatible matrices A, B. Loops and Vectorization. In Matlab, the code has been optimized so that most operations involve vectorization which is generally faster than loops. However, loops remain an important part of computing and an approach for which you should have significant expertise. In this exercise we will look at four problems that you have solved previously using Matlab and you will now implement its equivalent using loops. Note - you will have to think carefully to solve these problems - I suggest you try earnestly before consulting others, to test your logic skills ia. Jeopardy Previously you have solved the jeopardy problem in which 5 values from 200 to 1000 was repeated into 6 columns Sample Matlab code was range - linspace (200, 1000,5); mult - ones (1.6) Jep - range. mult You are to replicate this jeopardy matrix jep using only nested loops. ib. Meshgrid Previously meshgrid was introduced as a way to multiply two vectors of arbitrary length allowing for all possibilities in cach vector. Sample Matlab code was - 114 b- (5:101: (A,B)-magrid(a,b): Prod - A.B Given the same initial vectors a and b, replicate Prod using only nested loops. 1c. Reshape Previously you reshaped a column vector into a rectangular matrix using the reshape command. For example, if we start with a sample vector which is 20 clements long, then we can reshape to a 5 x 4 (or 4 x 5 or 10 x 2 or 2 x 10) using Matlab code such as: Vec - 11.2019; MSN Revec - zeshape (Vec, (MN) Given the same intial vector Vec, replicate ReVec using only nested loops. You code should work for any properly allowed values M and N where M'N original length of Vec. Also, in this example create an initial vector which is quite long (e.g. 20,000 elements) and use tic and toc in your code to estimate the time used by reshape and the time used by your code. Definitely avoid printouts in this exercise. Which is faster and by how much? id. Matrix Multiplication Matrix Multiplication of an M x P matrix (A) with a Px N matrix (B) yields an Mx N matrix (C) with the Matlab command: C-AB Replicate this result by using three nestod loops. Your code should work for any compatible matrices A, B

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions