# Introduction to MATLAB Cody Solutions

Learn Matlab and earn a badge and skill. This section contains the Cody solution of Introduction to MATLAB Cody Solution. Total 24 solved problems.

Visit the Matlab official Problems page of Introduction to Matlab.

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.

Get Notified Faster

## Problem 8. Add two numbers

Given a and b, return the sum a+b in c.
``````function c = add_two_numbers(a,b)
c = a + b;
end``````

## 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
``````function y = vecsum(x)
y=sum(x);
end``````

## Problem 1702. Maximum value in a matrix

Find the maximum value in the given matrix.
For example, if
A = [1 2 3; 4 7 8; 0 9 1];
then the answer is 9.
``````function y = your_fcn_name(x)
y = max(max(x));
end``````

## Problem 1545. Return area of square

Side of square=input=a
Area=output=b
``````function b = area_square(a)
b = a.^2;
end``````

## Problem 23. Finding Perfect Squares

Given a vector of numbers, return true if one of the numbers is a square of one of the numbers. Otherwise return false.
Example:
Input a = [2 3 4]
Output b is true
Output is true since 2^2 is 4 and both 2 and 4 appear on the list.
``````function b = isItSquared(a)
for i = 1:length(a)
a2 = a(i)^2;
for j = 1:length(a)
if a2 == a(j)
b = true;
break
else
b = false;
end
end
if b == true
break
end
end
end``````

## Problem 2. Make the vector [1 2 3 4 5 6 7 8 9 10]

In MATLAB, you create a vector by enclosing the elements in square brackets like so:
x = [1 2 3 4]
Commas are optional, so you can also type
x = [1, 2, 3, 4]
Create the vector
x = [1 2 3 4 5 6 7 8 9 10]
There’s a faster way to do it using MATLAB’s colon notation.
``````function x = oneToTen
x = 1:10;
end``````

## Problem 1035. Generate a vector like 1,2,2,3,3,3,4,4,4,4

Generate a vector like 1,2,2,3,3,3,4,4,4,4
So if n = 3, then return
[1 2 2 3 3 3]
And if n = 5, then return
[1 2 2 3 3 3 4 4 4 4 5 5 5 5 5]
``````function y = your_fcn_name(x)
y = [];
for x = 1:x
y = [y,x*ones(1,x)]
end
end``````

## Problem 5. Triangle Numbers

Triangle numbers are the sums of successive integers. So 6 is a triangle number because
6 = 1 + 2 + 3
which can be displayed in a triangular shape like so
*
* *
* * *
Thus 6 = triangle(3). Given n, return t, the triangular number for n.
Example:
Input n = 4
Output t is 10
``````function t = triangle(n)
t = sum(1:n);

end``````

## Problem 2015. Length of the hypotenuse

Given short sides of lengths a and b, calculate the length c of the hypotenuse of the right-angled triangle.

``````function c = hypotenuse(a,b)
c = sqrt(a^2+b^2)
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]
``````function y = everyOther(x)
y=[];
for i=1:2:length(x)
y=[y x(i)];
end``````

## Problem 233. Reverse the vector

Reverse the vector elements.
Example:
Input x = [1,2,3,4,5,6,7,8,9]
Output y = [9,8,7,6,5,4,3,2,1]
``````function y = reverseVector(x)
y = flip(x);
end``````

## Problem 7. Column Removal

Remove the nth column from input matrix A and return the resulting matrix in output B.
So if
A = [1 2 3;
4 5 6];
and
n = 2
then B is
[ 1 3
4 6 ]
``````function B = column_removal(A,n)
A(:,n)= [];
B = A;
end``````

## Problem 262. Swap the input arguments

Write a two-input, two-output function that swaps its two input arguments. For example:
[q,r] = swap(5,10)
returns q = 10 and r = 5.
``````function [q,r] = swapInputs(a,b)
q = b;
r = a;
end``````

## Problem 19. Swap the first and last columns

Flip the outermost columns of matrix A, so that the first column becomes the last and the last column becomes the first. All other columns should be left intact. Return the result in matrix B.
If the input has one column, the output should be identical to the input.
Example:
Input A = [ 12 4 7
5 1 4 ];
Output B is [ 7 4 12
4 1 5 ];
``````function B = swap_ends(A)
x = A(:,1);
y = A(:,end);
A(:,1) = y;
A(:,end) = x;

B = A;
end``````

## Problem 838. Check if number exists in vector

Return 1 if number a exists in vector b otherwise return 0.
a = 3;
b = [1,2,4];
Returns 0.
a = 3;
b = [1,2,3];
Returns 1.
``````function y = existsInVector(a,b)
y = length(b(b == a)) > 0;
end``````

## Problem 10. Determine whether a vector is monotonically increasing

Return true if the elements of the input vector increase monotonically (i.e. each element is larger than the previous). Return false otherwise.
Examples:
Input x = [-3 0 7]
Output tf is true
Input x = [2 2]
Output tf is false
``````function tf = mono_increase(x)
tf=true;
for k=1:length(x)-1
if x(k)>=x(k+1)
tf=false;
break
end
end``````

## Problem 645. Getting the indices from a vector

This is a basic MATLAB operation. It is for instructional purposes.
You may already know how to find the logical indices of the elements of a vector that meet your criteria.
This exercise is for finding the index of indices that meet your criteria. The difference is this:
vec = [11 22 33 44];
thresh = 25;
vi = (vec > thresh)
vi = 0 0 1 1

What we are looking for now is how to get the values
x = 3 4
Because those are the indices where the binary comparison is true.
Check out find.
Given a vector, vec, return the indices where vec is greater than scalar, thresh.
``````function out = findIndices(vec, thresh)
out = find(vec>thresh);
end``````

## Problem 33. Create times-tables

At one time or another, we all had to memorize boring times tables. 5 times 5 is 25. 5 times 6 is 30. 12 times 12 is way more than you think.
With MATLAB, times tables should be easy! Write a function that outputs times tables up to the size requested.
Example:
Input n = 5
Output m is
[ 1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25 ]
``````function m = timestables(n)
for i = 1 : n
for j = 1 : n
m(i, j) = i * j
end
end
end``````

## Problem 649. Return the first and last characters of a character array

Return the first and last characters of a character array, concatenated together. If there is only one character in the character array, the function should give that character back twice since it is both the first and last character of the character array.
Example:
stringfirstandlast(‘boring example’) = ‘be’
``````function y = stringfirstandlast(x)
y = [x(1) x(end)]
end``````

## Problem 568. Number of 1s in a binary string

Find the number of 1s in the given binary string. Example. If the input string is ‘1100101’, the output is 4. If the input string is ‘0000’, the output is 0
``````function y = one(x)
y=0
for i=1:length(x)
if x(i)=='1'
y=y+1
end
end
end``````

## Problem 174. Roll the Dice!

Description
Return two random integers between 1 and 6, inclusive, to simulate rolling 2 dice.
Example
[x1,x2] = rollDice();
x1 = 5;
x2 = 2;
``````function [x1,x2] = rollDice()
x1=randi(6,1);
x2=randi(6,1);
end``````

## Problem 641. Make a random, non-repeating vector.

This is a basic MATLAB operation. It is for instructional purposes.
If you want to get a random permutation of integers randperm will help.
Given n, put the integers [1 2 3… N] in a random order.
Yes, the test suite is not conclusive, but it is pretty close!
``````function vec = makeRandomOrdering(n)
vec = randperm(n);
end``````

## Problem 1087. Magic is simple (for beginners)

Determine for a magic square of order n, the magic sum m. For example m=15 for a magic square of order 3.
``````function m = magic_sum(n)
m=sum(magic(n),1);
m=m(1);

end``````

## Problem 189. Sum all integers from 1 to 2^n

Given the number x, y must be the summation of all integers from 1 to 2^x. For instance if x=2 then y must be 1+2+3+4=10.
``````function y = sum_int(x)
y = sum([1:2^x]);
end``````

Introduction to MATLAB 24 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.

0