Question

let us create a variable for a row vector a = [1, 4, 1, 3, 2,...

let us create a variable for a row vector a = [1, 4, 1, 3, 2, 5, 0] and calculate the mean value of its elements using the Matlab function ‘mean’ and store this value in variable aMean. Fig. 1 gives the Matlab code to do this.

                   a = [1, 4, 1, 3, 2, 5, 0];
                   aMean = mean(a);

Figure 1: Matlab code – row vector and mean of its elements.

Let us now construct a row vector b that contains the indices of elements of a which are larger than the aMean. The code in Fig. 2 performs this by using a ‘for’ loop. Now, add a line into the code to construct a row vector c that contains the values of elements from a which are larger than the mean of a.

              %  With a ‘for’ loop
              j=0;
              for i=1:length(a),

1 1.1

    if a(i)>aMean
        j=j+1;

b(j)=i;

end end

% add line here to construct the row vector c

Figure 2: Matlab code (‘icme Lab1 vectorMan Sept2020 fin.m’) – manipulation of vectors.

Finally, the code in Fig. 3 perform the same as above but using logical operators and ‘find’ function without any use of ‘for’ loop.

The results should be: aM ean = 2.2857, b = (2, 4, 6), c = (4, 3, 5). 1

% Without a ‘for’ loop b_noLoop = find(a > aMean);

Figure 3: Matlab code – manipulation of vectors (logical operators and ‘find’).

Homework Answers

Answer #1

code:

output:

raw_code :

a = [1,4,1,3,2,5,0]; %row vector a
aMean = mean(a); %mean of a

%for loop for finding indices
j=0;
for i=1:length(a)
if(a(i)>aMean)
j=j+1;
b(j) = i;
end
end

%to construct the row vector c
c = a(b)
code :

output :

raw_code :

a = [1,4,1,3,2,5,0]; %given data
aMean = mean(a);

b = find(a>aMean); %to find indices without loop
c = a(b)

**do comment for queries and rate me up******

Know the answer?
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
Similar Questions
MATLAB Create a matrix E, using A and B vectors as row 1 and row 2...
MATLAB Create a matrix E, using A and B vectors as row 1 and row 2 respectively A = 10 thru 1 B = 1 thru 4.2 with ten equally spaced elements and Find the indices (row and col) within E where (prob02a, b, c, d) E = 5 E > 4 E < 1.9 E > 1 and E < 2
For this assignment you need to write a parallel program in C++ using OpenMP for vector...
For this assignment you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is: #pragma...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector...
For this assignment, you need to write a parallel program in C++ using OpenMP for vector addition. Assume A, B, C are three vectors of equal length. The program will add the corresponding elements of vectors A and B and will store the sum in the corresponding elements in vector C (in other words C[i] = A[i] + B[i]). Every thread should execute an approximately equal number of loop iterations. The only OpenMP directive you are allowed to use is:...
1. In the following C code, elements of each row are stored contiguously in memory. Assume...
1. In the following C code, elements of each row are stored contiguously in memory. Assume each element in an array is a 32-bit integer. p = 0; for (j=0; j<8; ++j) for(i=500; i>0; --i ) {     A[ i] = 2 * B[j]; ++p; } a. How many 32-bit integers can be stored in a 32-byte cache block? b. Which variable references exhibit temporal locality? c. Which variable references exhibit spatial locality?
Here are some vectors in R 4 : u1 = [1 3 −1 1] u2 =...
Here are some vectors in R 4 : u1 = [1 3 −1 1] u2 = [1 4 −1 1] u3 = [1 0 −1 1] u4 = [2 −1 −2 2] u5 = [1 4 0 1] (a) Explain why these vectors cannot possibly be independent. (b) Form a matrix A whose columns are the ui’s and compute the rref(A). (c) Solve the homogeneous system Ax = 0 in parametric form and then in vector form. (Be sure the...
x y s t P 1 -3 1 0 0 12 1 2 0 1 0...
x y s t P 1 -3 1 0 0 12 1 2 0 1 0 3 -6 -4 0 0 1 0 The pivot element for the initial simplex tableau show is the red 1. So we need to zero out the other elements of column x. What is the formula used to zero out row 1 and column x? Multiply Row _____by_______ and then add the result to Row_____ What is the formula used to zero out row...
In the code space provided in Matlab Grader, create a function that: 1. Accepts an input...
In the code space provided in Matlab Grader, create a function that: 1. Accepts an input matrix A as its only input argument. Matrix A is a 3x3 matrix of random integers between 1 and 4 2. Produces and output matrix B as its only output argument. Matrix B will also be a 3x3 matrix. 3. Calculates the elements of matrix B such that: a. the values of B along the diagonal are equal to the respective elements in A...
1: A) Given the following vectorized code: >>x=[1:10]; >>f=x.^2+2; Rewrite this code using a for loop...
1: A) Given the following vectorized code: >>x=[1:10]; >>f=x.^2+2; Rewrite this code using a for loop that defines each element of f one at a time. Make sure to preallocate memory to f before filling each spot. B) See the following code. Rewrite the code in one line using the find function and a For loop. then write it again using a while loop x=[-1 3 7 2 4 0]; v=[]; for i=1:length(x) if x(i)<=2 v=[v, x(i)]; end end please...
A (–4, –1, 2), B (3, –2, –1) and C (–1, 3, –4), AB= 7? −...
A (–4, –1, 2), B (3, –2, –1) and C (–1, 3, –4), AB= 7? − ? − 3? CB = 4? − 5? + 3? AC = 3? + 5? - 2? Question 7: Express the vector AC as the sum of two vectors: AC = ? + ?, where ? is parallel to the vector CB and ? is perpendicular to CB. Given that AC ∙ CB = −26 and that CB = √50, determine the y-component of...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT