Matlab Basics on Vectors Cody Solution

Matlab Basics on Vectors Cody Solutions

Learn Matlab and earn a badge and skill. This section contains the Cody solution of MATLAB Basics on Vectors Solution. Total 11 solved problems.

Visit the Matlab official Problems page of Matlab Basics on Vectors.

All solutions were verified and working. It may be possible after a year or more community may modify the question, then the provided solution may not work so do comment below about the same. Our team will suddenly work on it.

JOIN US ON TELEGRAM
Get Notified Faster

Problem 3. Find the sum of all the numbers of the input vector

Find the sum of all the numbers of the input vector x.

Examples:
Input x = [1 2 3 5]
Output y is 11

Input x = [42 -1]
Output y is 41

Output the sum of the given vector.

function ans = vecsum(x)
   sum(x)
end

Problem 6. Select every other element of a vector

Write a function which returns every other element of the vector passed in. That is, it returns the all odd-numbered elements, starting with the first.

Examples:
Input x = [1 3 2 4 3 5]
Output y is [1 2 3]

Input x = [5 9 3 2 2 0 -1]
Output y is [5 3 2 -1]

Returns a vector consisting of odd subscripts of the input vector.

function y = everyOther(x)
  y = x(1:2:end);
end

Problem 247. Arrange Vector in descending order

If x=[0,3,4,2,1] then y=[4,3,2,1,0]

Sort the array from large to small, which can be sorted using sort similar to c++:

function y = desSort(x)
  y = flipdim(sort(x),2);
end

Problem 135. Inner product of two vectors

Find the inner product of two vectors.

Return the inner product of two vectors, do the dot multiplication of the two vectors and then sum them.

function z = your_fcn_name(x,y)
  z = sum(x.*y);
end

Problem 624. Get the length of a given vector

Given a vector x, the output y should equal the length of x.

Output the size y of the vector x, using the length function.

function y = VectorLength(x)
  y = length(x);
end

Problem 1107. Find max

Find the maximum value of a given vector or matrix.

Find the maximum value of a given matrix or vector.

function y = your_fcn_name(x)
  y = max(max(x));
end

Problem 605. Whether the input is vector?

Given the input x, return 1 if x is vector or else 0.

function y = checkvector(x)
  y = isvector(x);
end

Problem 2631. Flip the vector from right to left

Flip the vector from right to left.

Examples
x=[1:5], then y=[5 4 3 2 1]
x=[1 4 6], then y=[6 4 1];

Request not to use direct function.

function y = flip_vector(x)
  y=flipdim(x,2);
end

Problem 3076. Create a vector

Create a vector from 0 to n by intervals of 2.

Return a vector from 0 to n with an interval of 2. Use 0:2:n directly.

function y = zeroToNby2(n)
  y =0:2:n;
end

Problem 1024. Doubling elements in a vector

Given the vector A, return B in which all numbers in A are doubling. So for:

A = [ 1 5 8 ]
then
B = [ 1 1 5 5 8 8 ]

function B = your_fcn_name(A)
    B=zeros(1,2*length(A));
    B(1:2:end)=A;
    B(2:2:end)=A;
end

Problem 42651. Vector creation

Create a vector using square brackets going from 1 to the given value x in steps on 1.

Hint: use increment.

Create a vector with a step length of 1 and 1 to x, using square brackets

function y = vector(x)
  y = 1:x;
end

Matlab Basics on Vectors Cody 11 solved problems. All solution is correct as they were first submitted in Matlab and then uploaded here for your help. If any solution doesn’t work then do comment.

Also Check the solution of other Matlab Cody Contest Matlab Cody More Contest solutions.

Leave a Comment

Your email address will not be published.

Tech Info