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

  • 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”.

  • 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...

  • Assignment Develop a program to analyze one or more numbers entered by a user. The user...

    Assignment Develop a program to analyze one or more numbers entered by a user. The user may enter one or more numbers for analysis. Input should be limited to numbers from 1 through 1000. Determine if a number is a prime number or not. A prime number is one whose only exact divisors are 1 and the number itself (such as 2, 3, 5, 7, etc.). For non-prime numbers, output a list of all divisors of the number. Format your...

  • Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program...

    Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program will calculate and display the total of all numbers up to a specific number (entered by the user). Only positive numbers are allowed. Part B: User-controlled loop Part A Input Validation loop Part A: Summation loop Examples: When 5 is entered the program will calculate the total as 1+2+...+5 and display 15. When 2 is enterered the program will calculate the total as 1+2...

  • MATLAB ONLY PLEASE...ALSO AS SIMPLE AS POSSIBLE...NEW TO LANGUAGE Write a code that will ask the...

    MATLAB ONLY PLEASE...ALSO AS SIMPLE AS POSSIBLE...NEW TO LANGUAGE Write a code that will ask the user to input a positive number x. If the number is negative the program will keep asking again and again (as long as the value is negative) until the user inputs a positive value. At the end of the program, calculate the following: y = e-x

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