Question


Problem 2: Matrix Vector Operations [5pt] a) Create a program named my_dot Product which USING LOOPS calculates the dot prod-

Problem 2: Matrix Vector Operations

a) Create a program named my_dotProduct which USING LOOPS calculates the dot product of two vectors (of dimensions \(1 \times n\) ) from the keyboard. The program should check the input from the user making sure that the vectors are the right dimensions. If the vectors are not then an error message should be outputted to the user. If the vectors are of the right dimensions then the answer should be outputted to the user. Using your code, complete the following table:

b) Create a program named my_arrayCount which inputs an \(n \times m\) array and determines (USING LOOPS) how many elements in the array are greater than zero and how many are less than zero. The program should then output the answers. Using your code, complete the following table:

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

Below are the code in MATLAB for the above questions:

(my_dotProduct.m)

clear,clc;
%taking input vector u,v from user.
u = input('Enter the elements of vector u: ');
v = input('Enter the elements of vector v: ');

%storing length of both vectors in n and m respectively.
n=length(u);
m=length(v);

%checking for dimension mismatch.
if(n==m)
%variable result that stores dot product of vector u and v.
result = 0;
%finding dot product.
for i = 1 : n
result = result + u(i)*v(i);
end
%displaying result.
fprintf('u.v = %d\n',result);
else
%displays error if vector mismatch.
disp('Error:vector dimension mismatch');
end

(my_arrayCount.m)

clear,clc;
%taking nxm array input from the user.
arr = input('enter the size of 2D array: ');
%storing dimension of array arr.
[n, m] = size(arr);
%variables which stores count of negative and
%positve numbers in an array arr.
neg = 0;
pos = 0;

%iterating in the array.
for i = 1:n
for j = 1:m
%array is less than zero then increment neg.
if(arr(i,j)<0)
neg = neg + 1;
else
%else we increment pos.
pos = pos + 1;
end
end
end
%displaying count of pos and neg in the array arr.
fprintf('n(>0): %d\nn(<0): %d\n',pos,neg);

Refer to the screenshot attached below to better understand the code and indentation:

(my_dotProduct.m)
1- my_dotProduct. m x my_arrayCount. m x + clear, clc; ftaking input vector u,v from user. u = input(Enter the elements of v
Output:
Enter the elements of vector u: (3 -24) Enter the elements of vector v: [2 2 -3] u.v = -10

Enter the elements of vector u: 0 0 -1] Enter the elements of vector v: [1 0 0] u.v = 0

Enter the elements of vector u: [2 1 2] Enter the elements of vector v: (-1 2 5] u.v = 10
a)
--- -------------------- IV ----- I u.V 31-2j+4k | 2i+2j-3k | -10 0i+0j-1k | 11-6j+ek o 2i+1j+2k -1i+2j+5k | 10


(my_arrayCount.m)
my_dotProduct.m x my_arrayCount. m x + clear, clc; Staking nxm array input from the user. arr = input(enter the size of 2D a
Output:
enter the size of 2D array: (3,2,-1,5,0,-2,-3] n>0: 4 n<0: 3

enter the size of 2D array: n>0: 2 n<0: 2

enter the size of 2D array: [1 -2 3; 2 10 -10; -pi 5 3; -5 17 20] n>0: 8 n<0: 4

b)
Array n(>0) n(<0) [3,2,-1,5,0-2-3] [H1 2 3 2 -10 -12 - 53 5 17 20]

If my answer helps you then please Upvote,
for further queries comment below.


Thank You.

Add a comment
Know the answer?
Add Answer to:
Create a program named my_dotProduct which USING LOOPS calculates the dot product of two vectors
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
  • in c++ Write a program which can find the dot product of two vectors of the...

    in c++ Write a program which can find the dot product of two vectors of the same length ?. The user will enter the length ?. Use the length to control how many times you loop. The result is a scalar value and not a vector. If the dot product is zero, then the two vectors are perpendicular.

  • using C# Write a program which calculates student grades for multiple assignments as well as the...

    using C# Write a program which calculates student grades for multiple assignments as well as the per-assignment average. The user should first input the total number of assignments. Then, the user should enter the name of a student as well as a grade for each assignment. After entering the student the user should be asked to enter "Y" if there are more students to enter or "N" if there is not ("N" by default). The user should be able to...

  • Please write the code using matlab 1) i. Create an anonymous function named optimist, that accepts...

    Please write the code using matlab 1) i. Create an anonymous function named optimist, that accepts a single variable as input i Create a row vector Tthat represents the number of seconds in two minutes, starting ii. Call the function optimist on the vector T, store the result in variable R1 and returns the double of that variable from one and ending at a hundred and twenty v. Create an anonymous function named pessimist, that accepts a single variable as...

  • C++ Create a program that finds the dot product of two vectors. I'm currently trying to...

    C++ Create a program that finds the dot product of two vectors. I'm currently trying to display the dot product by calling the dotProduct member function however I am confused as to how to do this. What would be the proper way to display the dot product? I don't believe my dotProduct member function is set up correctly to access the proper data. Feel free to modify the dotProduct member function to allow for it to work with the other...

  • In Python Exercise – For & While Loops Create a new file called loops.py and use...

    In Python Exercise – For & While Loops Create a new file called loops.py and use it for all parts of this exercise. Remember the difference between input and raw input? Be sure to test your code for each part before moving on to the next part. Write a program using a for loop that calculates exponentials. Your program should ask the user for a base base and an exponent exp, and calculate baseexp. Write a program using a while...

  • Write a Java program that calculates the sum of a variable sized matrix using a two...

    Write a Java program that calculates the sum of a variable sized matrix using a two dimensional array. (ArraySum.java) The user should provide the number of rows and columns Test multiple user inputs (3 times 2, 4 times 4, 6 times 2, etc) A sum should be created for each row, each column, and the total for the matrix Ex.: How big would you like your matrix? Rows - ? 3 Columns -? 2 Please enter your row 1? Column...

  • 1) Create a matlab function that calculates: for Name your function TwoVarFunc : The inputs should...

    1) Create a matlab function that calculates: for Name your function TwoVarFunc : The inputs should be in the same order: xmin xmax ymin ymax N: n is the number of elements in the vector x and y The ouputs should be in the same order: f_xy: is the calculated array f(x,y) f_xyMAX: should be the the maximum value of the function f(x,y). Hint: to create vectors x and y look up the matlab built-in function linspace() Hint 2: To...

  • Java arrays Create method named repeatedN that takes two integer parameters named range and n and...

    Java arrays Create method named repeatedN that takes two integer parameters named range and n and returns an integer array Method should return an integer array that has numbers 0 - range repeated in order n times If range is less than or equal to 0; return an array that has 0 repeated n times Create a second method named printArray that takes an integer array as a parameter. The method should print out every element on the same line...

  • Use Python and only while loops For this part of the homework you will write code...

    Use Python and only while loops For this part of the homework you will write code to draw an isosceles right triangle. (A right triangle in which the height and base are the same size.) Your program should prompt the user for these inputs, in exactly this order: 1. The height of their triangle 2. The symbol the triangle will be outlined in 3. The symbol the triangle will be filled with For these inputs, you can assume the following:...

  • Design a program using a RAPTOR flowchart that has two parallel arrays: a String array named...

    Design a program using a RAPTOR flowchart that has two parallel arrays: a String array named people that is initialized with the names of twenty of your friends, and a String array named phoneNumbers that is initialized with your friends’ phone numbers. The program should allow the user to enter a person’s name. It should then search for that person in the people array. If the person is found, it should get that person’s phone number from the phoneNumbers array...

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