Question

In: Computer Science

• In this script, write MATLAB code to create an array A of size 100 ×...

• In this script, write MATLAB code to create an array A of size 100 × 3. The first column should contain random numbers between 0 and 10. The second column should also contain random numbers between 0 and 10. The third column should contain random integers between 20 and 50. The first two columns represent the x and y coordinates of a map, respectively. The third column represents the height of buildings. For example, if the first row of A is equal to [3.4 4.5 28], this means that there is a building of height 28 at location (3.4, 4.5) in the map. Because array A has 100 rows, there will be 100 buildings located at random points in the map. • Then, save array A in a file using the MATLAB function save. You may select the name of the file yourselves. • Then, plot the first column of array A vs the second column of array A, using ‘*’ (i.e., the points will be plotted as stars) so that you can see the 100 locations of the buildings. • Finally, after you save the array, clear all variables by placing a clear all at the end of the MATLAB script. • In this script, write MATLAB code to first read the file that you saved in Part A. To read the file, use the MATLAB function load. After you read the file, array A should again become available to you. • Then, write code to move the data from array A to a cell array C of size 10 × 10 in the following way: ◦ The element C{1,1} in C should be an array containing all building heights with x coordinates between 0 and 1 AND y coordinates between 0 and 1. ◦ The element C{2,1} in C should be an array containing all building heights with x coordinates between 1 and 2 AND y coordinates between 0 and 1. ◦ The element C{1,2} in C should be an array containing all building heights with x coordinates between 0 and 1 AND y coordinates between 1 and 2. ◦ The element C{2,2} in C should be an array containing all building heights with x coordinates between 1 and 2 AND y coordinates between 1 and 2. ◦ The element C{3,2} in C should be an array containing all building heights with x coordinates between 2 and 3 AND y coordinates between 1 and 2.

Solutions

Expert Solution

% '%%' represents a section. Each section can be independently run using
% the run section option
%% Part A
% rand(c,r) function returns a matrix of size c x r of random numbers
% between 0 and 1. So we just multiply the funciton output by 10 to get
% random integers between 0 an 10
c1 = 10*rand(100,1); % column 1
c2 = 10*rand(100,1); % column 2
% randi([a b], c, r) function returns a matrix of size c x r of random
% integers between a and b
c3 = randi([20 50],100,1); % column 3
% given 3 column vectors a, b, c the command [a b c] returns a matrix with
% columns as a, b, c
A = [c1 c2 c3];
% save('filename.mat','A') saves the matrix A in the current directory under
% the name given by filename
save('filename.mat','A');
% Given a matrix A A(:,i) returns the ith column
% plot(x,y,'*') plots y vs x using '*'
% here we want to plot column 2 vs column 1
plot(A(:,1),A(:,2),'*');
clear all;

%% Part B
% While saving in a file matlab saves the variables as a struct. To access
% the variable named A first we load the struct saved using the
% load('filename.mat') command
Astruct = load('filename.mat');
% Now we extract the matrix A from the struct using struct.A
A = Astruct.A;
% Initialise C
% Loop over entries of C
% Decompose A to individual arrays
c1 = A(:,1);
c2 = A(:,2);
c3 = A(:,3);
for i = 1:10
for j = 1:10
% We will use the fact that C(a<A & A<b & c<B & B<d) gives us the
% values of C where a<A<b and c<B<d
C{i,j} = c3(i-1 <= c1 & c1 < i & j-1 < c2 & j < c2);
end
end


Related Solutions

MATLAB, if-else Write a script which to do the following. Take an array of any size...
MATLAB, if-else Write a script which to do the following. Take an array of any size as input and check if all the elements are integers. (i) If not, display the error message ‘Invalid input!’. Use the error command to create a proper error message in red. (ii) If yes, count the number of rows where the sum of the elements is smaller than the average row sum. It is recommended that you use a test variable with many rows...
Write the MATLAB code to Create a new figure. Define a theta array such that ?...
Write the MATLAB code to Create a new figure. Define a theta array such that ? 2 ≤ ? ≤ 9? 2 with increment of ? 10 ; Create a sixmember array of ones called r. Create a new polar plot of ? versus r
Using Matlab, write code that carries out the following instructions: **Within your Live Script, create a...
Using Matlab, write code that carries out the following instructions: **Within your Live Script, create a matrix A by starting with eye(5) and performing the following sequence of elementary row-operations: first, replace (row4) with [(row4) + (row2) ⋅3], then, interchange rows 1 and 3 of the obtained matrix, and, finally, scale row5 of the matrix from the previous step by 6 to get the output matrix A. Display A. Note: To complete this part, you, should, first, create (and output)...
(MATLAB) Write a script that would create a 3x5 matrix of random integers and write this...
(MATLAB) Write a script that would create a 3x5 matrix of random integers and write this new matrix to a file called “Assignment3_Question5.dat”.
**MATLAB Code only Please create a MATLAB script that is a TIC-TAC-TOE game. Please keep it...
**MATLAB Code only Please create a MATLAB script that is a TIC-TAC-TOE game. Please keep it simple, but there is no other rules. Thank you.
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for...
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for a minimum and maximum real number, then generates and prints a random number in the requested range. The script should then do the same for an integer range. Sample output: Enter a minimum real value: 0.5 Enter a maximum real value: 30 A random number in the range ( 0.5000, 30.0000 ) is 1.7851 Enter a minimum integer value: -10 Enter a maximum integer...
In MATLAB Write a function to create an array of N numbers with a normal distribution....
In MATLAB Write a function to create an array of N numbers with a normal distribution. Call that from a script to create 1000 numbers, with a mean of 50 and a sigma of 10.
MATLAB CODE Let’s say you need to write a small script that takes in the total...
MATLAB CODE Let’s say you need to write a small script that takes in the total amount of money entered, and a cost, and returns the correct change in quarters/dimes/nickels/pennies. Initialize counter variables for quarters, dimes, nickels and pennies to 0. Counter variables are used to keep track of how many of each coin are to be returned. Calculate the amount of change to be given using the ‘total’ and ‘cost’ variables. While there is still change to be given,...
MATLAB CODE Let’s say you need to write a small script that takes in the total...
MATLAB CODE Let’s say you need to write a small script that takes in the total amount of money entered, and a cost, and returns the correct change in quarters/dimes/nickels/pennies. Initialize counter variables for quarters, dimes, nickels and pennies to 0. Counter variables are used to keep track of how many of each coin are to be returned. Calculate the amount of change to be given using the ‘total’ and ‘cost’ variables. While there is still change to be given,...
Write a R-script to (and show the outputs of your code) (a) Create a sequence of...
Write a R-script to (and show the outputs of your code) (a) Create a sequence of numbers starting at 3.5 and ending at 10.7 with increments of 0.79. Find the variance and mean of those numbers. And finally sort the vector in a decreasing manner (b) Create a 3 different 3 by 3 matrices such that each of the numbers 1,2,...,9 appear exactly once (Sudoku style) in each of the matrices.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT