********* USE MATLAB ******** 1. A 1x12 vector is given by x -3.5.5-6.2 11.1 07
ID: 2293752 • Letter: #
Question
********* USE MATLAB ********
1. A 1x12 vector is given by x -3.5.5-6.2 11.1 07 -9.5 2 15 -1 3 2.5]. Use loops to create two new vectors- one (call it P) that contains the positive elements of x, and a second (call it N) that contains the negative elements of x. In both P and N, the elements should be in the same order as in x. 2. Sexy primes are two prime numbers that the difference between them is 6. For example, 23 and 29 are sexy primes since 29-23-6. Use loops to find all the sexy primes between 1 and 400. Store all sexy primes in a 2-column matrix where the 2 columns correspond to the 2 numbers in each pair of primes. (ie: 23 gets stored in column 1 and 29 gets stored in column 2.) Display the completed matrix. You may not use the built- in function "isprime" 3. The following is a list of 20 exam scores: 69, 81,47, 71, 54, 76, 60, 92, 85, 43, 88, 80, 79, 69, 26, 82,89, 99, 71, 59. Calculate the average of the top 8 scores, and display this average in the command window to 1 decimal place. 4. The elements of the symmetric Pascal matrix are obtained from: Where i is the row number and j is the column number of the element. Use nested loops to create an 9x9 symmetric Pascal matrix. Display the completed matrix in the command window.Explanation / Answer
1.Ans)
=======================MATLAB CODE======================
x=[-3.5 5 -6.2 11.1 0 7 -9.5 2 15 -1 3 2.5];
c=0;%index for vector 'N'
k=0;%index for vector 'P'
for i=1:length(x) %loop through length of vector 'x'
if x(i)<0 %check whether number is negative
c=c+1;
N(c)=x(i);
else
k=k+1;
P(k)=x(i);
end
end
P
N
================================================
Result obtained when you run the above code
================
>> PandN
P =
Columns 1 through 7
5.0000 11.1000 0 7.0000 2.0000 15.0000 3.0000
Column 8
2.5000
N =
-3.5000 -6.2000 -9.5000 -1.0000
================================
as per chegg rules only 1 question per post will be answered please ,if common data question upto 4 parts will be answered
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.