Question

In: Computer Science

PART 3 – Using logical vectors and logical 2D arrays In this part, you will use...

PART 3 – Using logical vectors and logical 2D arrays

In this part, you will use a logical vector to make multiple decisions in parallel concerning values in two arrays. Firstly, generate two random [1x12] arrays using randi. The numbers should be integers between 1 and 5. Let’s call these two arrays a1 and a2. Your task is to generate a third array, a3, which at each index, contains a true/false value that defines whether the values in arrays a1 and a2 are NOT equal (use ~=). Now use the a3 array to obtain those values in a1 and a2 that are equal. You must accomplish all this using the logical vector and no loops.

Finally, create a random 5x5 numeric 2D array with numbers between 1 and 100 using one line of code. Replace all values divisible by 3 in this matrix with the value -1, again, using only one line of code.

Solutions

Expert Solution

% Matlab script to use logical vector and arrays

% generate two random [1x12] arrays using randi.
a1 = randi([1,5],1,12);
a2 = randi([1,5],1,12);
fprintf('a1 : ');
disp(a1);
fprintf('a2 : ');
disp(a2);

% generate a third array, a3, which at each index,
% contains a true/false value that defines whether
% the values in arrays a1 and a2 are NOT equal (use ~=).
a3 = (a1~=a2);
fprintf('a3 : ');
disp(a3);
% a1(a3==0)) returns the values from those indices in a1 where a3 is 0 i.e
% false (which means values at those indices in a1 and a2 are equal))
fprintf('a1 = a2 : ');
disp(a1(a3==0));

% create a random 5x5 numeric 2D array with numbers between 1 and 100
a = randi([1,100],5,5);
fprintf('a :\n');
disp(a);

%Replace all values divisible by 3 in this matrix with the value -1

a(mod(a,3) == 0) = -1;
fprintf('Modified a :\n');
disp(a);
%end of script

Output:


Related Solutions

In this assignment, you implement a 2D-matrix as a vector of vectors, and only use at()...
In this assignment, you implement a 2D-matrix as a vector of vectors, and only use at() to access its elements. Write a program that multiplies a 2D matrix with a vector. If you need to see the math, follow this link: https://mathinsight.org/matrix_vector_multiplication (Links to an external site.) For simplicity, our matrix will be of size 3 x 3. Initialize the matrix as shown in to become [1.0, 2.0, 3.0] [4.0 ,5.0 ,6.0] [7.0, 8.0, 9.0] Read the three values of...
In this problem, you will illustrate a geometric fact using vectors associated with 2D regions in...
In this problem, you will illustrate a geometric fact using vectors associated with 2D regions in 3D space. We start with the four points given as: A : (0, 0, 0) B : (1, 0, 0) C : (0, 1, 0) D : (0, 0, 1). These four points define a 3D object which is a tetrahedron (not a regular one, however). 1. Find an equation for the plane through points A, B, C; call this plane P1. Using this,...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 4: Calorie Counting Specifications: Write a program that allows the user to enter the number of calories consumed per day. Store these calories in an integer vector. The user should be prompted to enter the calories over the course of one week (7 days). Your program should display the total calories consumed over...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with...
You MUST use VECTORS in this lab. Do NOT use ARRAYS. Write code in C++ with //comments . Please include a screen shot of the output Part 1: Largest and Smallest Vector Values Specifications: Write a program that generates 10 random integers between 50 and 100 (inclusive) and puts them into a vector. The program should display the largest and smallest values stored in the vector. Create 3 functions in addition to your main function. One function should generate the...
To describe and analyze motion in 2D (or higher), the use of vectors is the common...
To describe and analyze motion in 2D (or higher), the use of vectors is the common method used. From your work with vectors, past and present, do you find the vector method a good (even best) way to analyze quantities in 2D? Are there other methods that could be used that will give the same results? From your own experience, what are the pros and cons in using the vector method? Please no handwritten or typed responses - only typed...
Need this C++ code to be modified to work in C, still using 2d arrays... #include...
Need this C++ code to be modified to work in C, still using 2d arrays... #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; //Implementation of main function int main() { srand(time(NULL)); //Declaration of output,result,i,j,k ,figure as integer type and //assign flog with 0 int output[5][5], result[5], i, j, k, figure = 0; //Display statement cout << "The classic BINGO cards contains 25 squares arranged in five vertical" << endl; cout << "columns and five side to side rows. Each...
Lab 7 - 2D Arrays (C++) In main, declare and fill a 2D array with one...
Lab 7 - 2D Arrays (C++) In main, declare and fill a 2D array with one hundred rows and fifty columns. Iterate through each element and assign it a random value between -72 and 75 inclusive. Have your random number seed be 25. Create functions that do the following: 1. A function called “sum” that returns the sum of all the elements in your 2D Array. 2. A function called “average” that return the average value of the elements in...
For C++ Use arrays and or vectors, no classes. Visualize and consider 100 lockers all lined...
For C++ Use arrays and or vectors, no classes. Visualize and consider 100 lockers all lined up horizontally in a row Each locker is numbered from 1 to 100 in sequential order Every locker can be fully closed (open state = 0.00) Every locker can be fully opened (open = 1.00) Every locker can be partially open with any possible value between 0.00 and 1.00 inclusive on both ends A locker cannot ever be more closed than fully closed (open...
For C++ Use arrays and or vectors, no classes. Visualize and consider 100 lockers all lined...
For C++ Use arrays and or vectors, no classes. Visualize and consider 100 lockers all lined up horizontally in a row Each locker is numbered from 1 to 100 in sequential order Every locker can be fully closed (open state = 0.00) Every locker can be fully opened (open = 1.00) Every locker can be partially open with any possible value between 0.00 and 1.00 inclusive on both ends A locker cannot ever be more closed than fully closed (open...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and...
C++ Make a Tic Tac Toe game for 2 players to play using 2D arrays and classes. Do not add more #include functions other than the ones listed. I never said what type of code I needed in a previous question. I apologize and I can't go back and change it so here is the same question with more information Using the tictactoeGame class, write a main program that uses a tictactoeGame to implement a game in which two players...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT