Question

In MATLAB, ask the user to input a matrix of any size. Ensure that the user...

In MATLAB, ask the user to input a matrix of any size. Ensure that the user inputs a positive, integer matrix. If there is an incorrect input, ask them for a matrix again. Use Loops to do this.

Using loops, find the sum of all prime numbers present in the user-entered matrix. You must not use the built-in isprime function to do this. Use loops. (The number 1 may count as as a prime number)

Example Matrix: [1 2 3; 4 5 6; 7 8 9]

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

a = input("Enter a matrix:\n"); % taking user input for the first time

while numel(a ( a < 0 )) > 0 % if negative numbers are in, asking again
a = input("Enter a matrix whithout negative numbers:\n");
end

[rows cols] = size(a); % taking size of the matrix

sum = 0;

for i = 1:rows % looping through rows
for j = 1:cols % looping through columns
  
number = a(i, j);
  
prime = 1; % checking if number is prime
for one = 2:number-1
if mod(number, one) == 0
prime = 0;
break;
end
end
  
if prime == 1 % if so, adding it to sum
sum += number;
end
  
end
end

sum % printing sum

% Please up vote. Happy Learning!

Add a comment
Know the answer?
Add Answer to:
In MATLAB, ask the user to input a matrix of any size. Ensure that the user...
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
  • MATLAB Write a function called next_prime that takes a scalar positive integer input n. Use a whi...

    MATLAB Write a function called next_prime that takes a scalar positive integer input n. Use a while-loop to find and return k, the smallest prime number that is greater than n. Feel free to use the built-in isprime function (do not use nextprime). MATLAB

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • JAVA Ask the user for integers. If the user did not enter an integer, ask again....

    JAVA Ask the user for integers. If the user did not enter an integer, ask again. (Type Safe Input) Keep a running total of the intergers entered (sum). Once the user enters 0, stop looping. Print the sum of all the numbers. Do not use try-catch. EXAMPLE OUTPUT: Enter an integer, 0 to stop> [fasdfsa] Invalid input. Enter an integer, 0 to stop> [231.342] Invalid input. Enter an integer, 0 to stop> [1] Enter an integer, 0 to stop> [2]...

  • write a Matlab program ( solve the three questions). please use array and create your own...

    write a Matlab program ( solve the three questions). please use array and create your own function to check primality Question 1 : [10 points) Write a MATLAB program that will store all the first 1000 prime numbers in an array variable named arr_of_primes. Please note that a prime number is a positive integer that is bigger than or equal to 2 which is divisible only by 1 and itself. Examples of the first 8 primes are: 2, 3, 5,...

  • Ask a user for three positive integer numbers. Use an input validation loop to make sure...

    Ask a user for three positive integer numbers. Use an input validation loop to make sure that all numbers are positive, ask again if not. Determine the smallest, middle and largest number and display them with proper labeling (Smallest=AA, Middle=BB, Largest=CC). Write this program in C

  • C language: P14. Ask a user for three positive integer numbers. Use an input valiadation loop...

    C language: P14. Ask a user for three positive integer numbers. Use an input valiadation loop to make sure that all numbers are positive, ask again if not. Determine the smallest, middle and largest number and display them with proper labeling (Smallest=AA, Middle=BB, Largest=CC).

  • 2. Write a program to do the following: • ask the user to input the size...

    2. Write a program to do the following: • ask the user to input the size of an array of integers and allocate space for the array • ask the user to input the integer values for the array and store these values into the array • calculate and display the sum and the average of elements of the array.

  • In Matlab, write a script which asks a user to input numbers until the user inputs...

    In Matlab, write a script which asks a user to input numbers until the user inputs 0. Then the script should print only the positive numbers entered by the user, in reverse order. For example, if the user inputs “4,-1,5,2,-3,-8,0” the script should print “2,5,4”.

  • Ask the user to input a scaler or a matrix then multiply the input by 2....

    Ask the user to input a scaler or a matrix then multiply the input by 2. Display the result with a proper explanation string in a single line using both the disp function and the fprintf function. please enter the value:[1 2 3] Ask the user to input a string (without the need to use single quotes). Use the fprintf function to store this string to a file.    can any one help please ? This is matlab language

  • matlab 1. Write a MATLAB user-defined function that finds the largest element(s) of a matrix. Name...

    matlab 1. Write a MATLAB user-defined function that finds the largest element(s) of a matrix. Name the function [max_value, max_index] - LASTNAMES matrix max (x), where input argument x is a vector or matrix (not a scalar), and the outputs are the maximum value(s) and indexes of the maximum value(s) of the mput x. Do not use MATLAB functions max() or find(). Use loops and if branches to determine the maximum elements of matrix. Check for correct number and valid...

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