CODY Contest 2020 Introduction to MATLAB 24 questions (Part 2)

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.

The value of the first element returned is equal to the second input, and the second returned is equal to the first input.

function [q,r] = swapInputs(a,b)
    q=b;
    r=a;
end

Question 14. 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 ];

The first and last column of the switching matrix:

function B = swap_ends(A)
    [m,n]=size(A);
    if(n>1)
        B = [A(:,end),A(:,2:end-1),A(:,1)];
    else
        B=A;
    end
end

 Divide into three parts: the last column, and the second column to the penultimate column, the first column, and then splice it. If A has a column, then return A directly.

Question 15 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.
Determine whether the vector b contains a.

You can use the find function to determine whether the find return is empty. If it is empty, then there is no value equal to b, and 1 is returned. Or iterate directly, return 1 if there is one, and never return 0 at the end.

function y = existsInVector(a,b)
  y=1-isempty(find(a==b));
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

Determine whether the vector is purely monotonically increasing, that is, the value of the i-th element is greater than the value of the i-1th element. It seems that there is no direct function to judge, then iterate, if it is not satisfied, directly return the value and return .

function tf = mono_increase(x)
  for i=2:length(x)
      if(x(i)<=x(i-1))
          tf=false;
          return ;
      end
  end
  tf=true;
end

Seventeenth question 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.

Returns the subscripts of all elements in the vector greater than Thresh, no, threshold.

Just use the find function directly.

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 ]

Given n, return a matrix of n*n columns, and the value increases from 1 to n^2 in the order of column first, row from left to right and top to bottom.

You can create a column vector and a row vector with a value of 1:n, and then multiply them to get.

function m = timestables(n)
  m=(1:n)'*(1:n);
end

1: n directly creates the row vector, and then use 'to transfer jobs to get the column vector.

第十九题 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'

Returns a string consisting of the first character of the input string and preferably one character.

function y = stringfirstandlast(x)
  y = [x(1) x(end)];
end

In the test example, the return value of input'a' is given as'aa'.

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

Count the number of occurrences of '1' in the string. From x=='1', you can get whether each character is equal to '1', equal to 1, and not equal to 0, and then sum to get the number equal to '1', or count The size of the find result is also ok.

function y = one(x)
  y=sum(x=='1');
end

或y=length(find(x=='1'));

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;

Consider the random number and randomly generate two values ​​from 1 to 6. Use rendi([m,n],[a,b]) to get a m*n random number matrix with values ​​from a to b (both inclusive).

function [x1,x2]= rollDice()
  ret=randi([1,6],[1,2]);
  x1=ret(1);
  x2=ret(2);
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!

Random sorting from 1 to n is troublesome to write by yourself, so use the randperm function directly.

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.

magic(n) is to generate an n*n Rubik's Cube matrix. The values ​​of each row, column, and diagonal are all equal and equal to (1+2+3+4+n^2)/n. If you don’t use it The characteristics of magic, then any sum of the diagonals of the rows and columns can be:

function m = magic_sum(n)
  m=sum(magic(n),1);
  m=m(1);
end

Or solve it directly according to the characteristics, and get m=n(n^2+1)/2.

Question 24 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.

Given n, find the sum of 1, 2, 3, 4,... 2^n, and use the summation formula of the geometric difference column to find:

function y = sum_int(x)
  y = (2^x)*(2^x+1)/2;
end

 

Guess you like

Origin blog.csdn.net/qq_36614557/article/details/110482526