Question

Write a program that can find the smallest element in a vector. Given a vector such...

Write a program that can find the smallest element in a vector. Given a vector such as [1 2 -2 7 -8 9 3 1], you program has to find its smallest element and output it in the command window. In the vector above, the smallest element would be the -8. Your program should work for any vector with any size. the answer should be answered by matlab.

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

MATLAB Code:

Output Snapshot:

Text Code:

clc;
clear all;
close all;
% Input Vector
inputVector = [1 2 -2 7 -8 9 3 1];

% Assigning first element of input vector as the smallest element
smallestElement = inputVector(1);
% to iterate through each element in vector except index 1
for i=2: length(inputVector)
% Comparing each element with smallestElement value
if inputVector(i)<smallestElement
% Assiging vector element to smallestElement when value is less
smallestElement = inputVector(i);
end
end
% Displaying the smallest element in the command window
disp(smallestElement)

Explanation:

  • Please refer code comments for the understanding purpose.
Add a comment
Know the answer?
Add Answer to:
Write a program that can find the smallest element in a vector. Given a vector such...
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
  • (Find the smallest element) use pointers to write a function that finds the smallest element in...

    (Find the smallest element) use pointers to write a function that finds the smallest element in an array of integers. Use {1,2,4,5,10,100,2,-22} to test the function. using the following header. int minindex(double* list, int size) example output: array size: 8 number 1: 1 number 2: 2 number 3: 4 number 4: 5 number 5: 10 number 6: 100 number 7: 2 number 8: -22 smallest element is: -22 C++ only.

  • Using matlab Write a function that receives a vector as an input argument and prints the...

    Using matlab Write a function that receives a vector as an input argument and prints the individual elements from the vector in a sentence format. One example of calling this function on the command window looks like >>printvec([1.12 23 9.245]) Element 1 is 1.12 Element 2 is 23.00 Element 3 is 9.25 1. (Note: your function should work for any vector.)

  • in C language Write a program to find the element of an array, which occurs maximum...

    in C language Write a program to find the element of an array, which occurs maximum number of times and its count. The program should accept the array elements as input from the user and print out the most occurring element along with its count. Hint: array size is 5. For example: Enter array elements (array size 5): 22321 Output: Max occurring element: 2. count: 3

  • 2. [5pts] Write a MATLAB script that can calculate the equivalent resistance in a circuit that all electrical resis...

    2. [5pts] Write a MATLAB script that can calculate the equivalent resistance in a circuit that all electrical resistors are connected in parallel: . Print the title 'Equivalent Resistance for Resistors Connected in Parallel' on your output. . Check the size of vector resistor to see how many resistors in the circuit. .Check each individual resistance to see any 0 or negative resistance. If there is any, print message 'ERROR', then finish the program. . Otherwise, calculate the equivalent resistance...

  • Use the Concept of vector and string to do the following question 1) Write a program...

    Use the Concept of vector and string to do the following question 1) Write a program that creates a vector of string called V. Vector V grows and shrinks as the user processes the transactions from a data file called “data.txt”. The transaction file can only include three commands: Insert, Delete and Print. With Insert command, you get two more information, the information you need to insert and the position it should be inserted. With Delete, you get one more...

  • Write a program that finds either the largest or smallest of the ten numbers as command-line...

    Write a program that finds either the largest or smallest of the ten numbers as command-line arguments. With –l for largest and –s for smallest number, if the user enters an invalid option, the program should display an error message. Example runs of the program: ./find_largest_smallest –l 5 2 92 424 53 42 8 12 23 41 output: The largest number is 424    ./find_largest_smallest –s 5 2 92 424 53 42 8 12 23 41 output: The smallest number is...

  • Below is a linear time complexity algorithm Max-Min-VER1 to find the biggest and smallest element a...

    Below is a linear time complexity algorithm Max-Min-VER1 to find the biggest and smallest element a given list. Input: A is a given list Output: two integers Example: Given A = {1, 5, 9, -3}. It returns -3 (the smallest element), and 9 (the biggest element) Max-Min-VER1(A, n) Line 1: large ← A[0] Line 2: for I from 1 to n - 1 do Line 3:     large ← Math.max(large, A[I]) Line 4: small ← A[0] Line 5: for I from...

  • Using PYTHON: (Find the index of the smallest element) Write a function that returns the index...

    Using PYTHON: (Find the index of the smallest element) Write a function that returns the index of the smallest element in a list of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: def indexOfSmallestElement(lst): Write a test program that prompts the user to enter a list of numbers, invokes this function to return the index of the smallest element, and displays the index. PLEASE USE LISTS!

  • i need this done in Matlab 8. Write a function which reverses a vector, i.e. if...

    i need this done in Matlab 8. Write a function which reverses a vector, i.e. if the input is (4, 2, 1,5,-3], the output would be [-3,5, 1, 2,4]. 9. Write a function which computes the sum of every second element in a vector.

  • C++ program Write a code segment to find the max element in each row and then...

    C++ program Write a code segment to find the max element in each row and then store the values to the "result" array. For example, { {2, 7, 9, 6, 4}, => 9 {6, 1, 8, 10, 4}, => 10 {4, 3, 7, 2, 9}, => 9 {9, 9, 0, 3, 1}, => 9 {8, 8, 7, 8, 9}, => 9 {1, 2, 1, 2, 3} } => 3 the result array has the values [9, 10, 9, 9, 9,...

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