Question

Write a Matlab program to store the first 4 perfect numbers in a row vector. Also...

  1. Write a Matlab program to store the first 4 perfect numbers in a row vector. Also create another column vector containing the same elements.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Create a MATLAB function called Perfect_numbers and then write the following code in it.

The code:

function Perfect_numbers()

i=2;c=0;x=0;

while(c<4)

s=0;

for d=1:i-1

if(rem(i,d)==0)

s=s+d;

end

end

if(s==i)

if(x==0)

x=i;

else

x=[x i];

end

c=c+1;

end

i=i+1;

end

y=x';

% The variable x stores the results in row vector form

% The variable y stores the results in column vector form

fprintf('\nFirst four perfect numbers in row vector: ');

fprintf('%d\t',x);

fprintf('\nFirst four perfect numbers in column vector:');

fprintf('\n%d',y);

end

On running the above code in the command window, the following output is generated :

First four perfect numbers in row vector: 6 28 496 8128
First four perfect numbers in column vector:
6
28
496
8128

Add a comment
Know the answer?
Add Answer to:
Write a Matlab program to store the first 4 perfect numbers in a row vector. Also...
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 ASSIGMENT 1. A. Create a function called ch16initials3 Input: row vector of 101 values (Note:...

    MATLAB ASSIGMENT 1. A. Create a function called ch16initials3 Input: row vector of 101 values (Note: input values are specified in the function call) Output: calculated maximum value Create a row vector A of values from 50 down to 0, with an increment of 0.5 Multiply the elements of A and the square of the elements of the input vector Store the results in B Find the maximum value of B and store it in C – this is the...

  • Write a for-loop in MATLAB that generates a vector of numbers such that each number is...

    Write a for-loop in MATLAB that generates a vector of numbers such that each number is the product of the previous two. Initialize the first two elements of your vector of numbers at the value of 1.1. In other words, "1.1" is the first element of the vector, and "1.1" is the second element of the vector. What is the 10th value in the vector?

  • MATLAB Onramp ASSIGNMENT: 2. Create a 1x15 (1 row, 10 columns) vector of random numbers (use...

    MATLAB Onramp ASSIGNMENT: 2. Create a 1x15 (1 row, 10 columns) vector of random numbers (use randn command “x = randn(1,10)). And do the following: a.Test vector x for elements that are less than 0.2. Assign the output to a variable named y. b.Create a variable z that contains the elements in x that are greater than 0.2. c. Modify vector x such that any value in it less than 0.2 is replaced by value 0.

  • Using Matlab Write a program which will: a. Accept two numbers from the user m and...

    Using Matlab Write a program which will: a. Accept two numbers from the user m and n b. Define an array with the dimensions a(n,m) and fill it with random numbers in the range of -100 to 100 inclusive. c. Provide the user the following menu from which he can select: 1. Sum of all elements in the array. Print the result. 2. Sum of the element in a row that he’ll specify. Print the result. 3. Sum of the...

  • Write a MATLAB program to do the following: Receive 5 input values and store them into...

    Write a MATLAB program to do the following: Receive 5 input values and store them into a single 1x5 array Receive another 2 input values and store them into a separate 1x2 array Add the scalar value 5 to each array value Store each array into 2 cells within one cell array Also store each array as separate elements within one struct

  • Please write a C++ Program for the following problem >> Vector: Calculation the sum of each...

    Please write a C++ Program for the following problem >> Vector: Calculation the sum of each two adjacent Numbers.... >>【Description】 Read integer numbers from keyboard and store them into a vector. Calculation the sum of each two adjacent Numbers, store the result into another vector. Then output the result inversely. For example, if the input is 1 2 3 4 5 then the output is 9 7 5 3 【 Input】 There are 5 test cases. For each case, there...

  • 2. Matrix A = Matrix B = log(A) Write MATLAB expressions to do the following. Evaluate...

    2. Matrix A = Matrix B = log(A) Write MATLAB expressions to do the following. Evaluate the sum of the first row of B Evaluate the maximum value in the vector resulting from element-by-element multiplication of the first column of B with the third column of A. Use element-by-element division to divide the third row of A by the first three elements of the second column of B and evaluate the sum of the elements of the resulting vector.

  • given a row vector x create each of the following row vectors as indicated y is...

    given a row vector x create each of the following row vectors as indicated y is two copies of x, one after another (a vector twice as large as x, containing two copies) z is a vector containing the elements of x with odd indices w is identical to x, except it contains the number 0 inserted before the first element of x, a 7 inserted after the fourth element of x, and 12 inserted after the last element of...

  • Write a script that will create a (random) vector of ages (real numbers, not integers). The...

    Write a script that will create a (random) vector of ages (real numbers, not integers). The script should contain the following prompts: - the user will provide the number of elements in the vector. - the user will provide the age of the oldest person in the group. For example: Enter the number of persons in the group: 50 Enter the age of the oldest person: 37 Save the vector containing the ages in a “.dat file” called ages_vector.dat matlab...

  • Assume that y is a column vector with ten elements, write matlab code that will create...

    Assume that y is a column vector with ten elements, write matlab code that will create a stem plot of the vector starting at a horizontal axis value of 1. Add code that will create the stem plot of the vector starting at a horizontal axis value of 0. Create additional code that creates a stem plot but plots y at every other horizontal value (i.e., 0, 2, 4, etc)

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