链接地址:
https://ww2.mathworks.cn/matlabcentral/cody/groups/78
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 11Input 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)
[r,c] = size(x)
y = x(1,1)
for i=1:r
for j=1:c
if x(i,j)>y
y = x(i,j)
else
continue
end
end
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 470. Scoring for oriented dominoes
Given a list of ordered pairs, and the order they should be placed in
a line, find the sum of the absolute values of the differences.list = [1 2
5 3
2 4order = [1 3 2]
yields: [1 2][2 4][5 3]
or: abs(2-2) + abs(4-5)
or: 0 + 1
or: 1
function score = scoreOrderedDominoes(list, order)
r = max(size(list))
sub = 0
for i=1:r
list_1(i,:) = list(order(i),:)
end
for j=1:r-1
sub = sub + abs( list_1(j+1,1)-list_1(j,2) )
end
score = abs(sub)
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 other numbers. Otherwise return false.Example:
Input a = [2 3 4]
Output b is trueOutput is true since 2^2 is 4 and both 2 and 4 appear on the list.
function b = isItSquared(a)
r = max(size(a))
q = 0
for i=1:r
for j=1:r
if a(j) == a(i)^2
q=q+1
break
end
end
end
if q > 0
b = true
else q == 0
b = false
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,2,3,4,5,6,7,8,9,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 ans = your_fcn_name(n)
p = 1
x = 1
q = ((n+1)*n)/2
for i = 1:n
for j = 1:x
ans(p) = x
p = p+1
end
x = x+1
end
end
Problem 5. Triangle Numbers
Triangle numbers are the sums of successive integers. So 6 is a
triangle number because6 = 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 = ((n+1)*n)/2;
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)
r = max(size(x))
p = 1
for i = 1:2:r
y(p) = x(i)
p = p+1
end
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)
r = max(size(x))
for i = 1:r
y(i) = x(r+1-i)
end
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)
[r,c] = size(A)
p = 1
for i = 1:r
for j = 1:c
if j == n
continue
else
B(i,p) = A(i,j)
p = p+1
end
end
p = 1
end
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)
[r,c] = size(A)
for i = 1:r
B(i,1) = A(i,c)
B(i,c) = A(i,1)
for j = 2:(c-1)
B(i,j) = A(i,j)
end
end
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)
r = max(size(b))
y = 0
for i = 1:r
if a == b(i)
y = 1
else
continue
end
end
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 trueInput x = [2 2]
Output tf is false
function tf = mono_increase(x)
r = length(x)
q = 0
for i = 1:(r-1)
if x(i) < x(i+1)
continue
else
q = 5
end
end
if q == 0
tf = true
else
tf = false
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)
vi = (vec > thresh)
p = 1
for i = 1:length(vi)
if vi(i) == 1
out(p) = i
p = p+1
else
continue
end
end
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
m(i,1) = i
for j = 2:n
m(i,j) = m(i,j-1) + i
end
end
end
Problem 649. Return the first and last character of a string
Return the first and last character of a string, concatenated
together. If there is only one character in the string, the function
should give that character back twice since it is both the first and
last character of the string.Example:
stringfirstandlast('boring example') = 'be'
function y = stringfirstandlast(x)
r = length(x)
a = x(1)
b = x(r)
y(1) = a
y(2) = b
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)
r = length(x)
y = 0
for i = 1:r
if x(i) == '1'
y = y+1
else
continue
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([1,6],1,1)
x2 = randi([1,6],1,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)
b = magic(n);
m = 0
for i=1:n
m = m + b(1,i)
end
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 = 0
for i=1:2^x
y = y + i
end
end