CODY Contest 2020 Introduction to MATLAB 全24题(下)

第十三题 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)
    [m,n]=size(A);
    if(n>1)
        B = [A(:,end),A(:,2:end-1),A(:,1)];
    else
        B=A;
    end
end

 分成三个部分:最后一列,而第二列到倒数第二列,第一列,然后拼接即可。如果A有一列,那么就直接返回A。

第十五题  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.
判断向量b中是否含有a。

可以用find函数,判断find返回是否为空,如果为空那么就没有值等于b,返回1。或者直接遍历,如果有就返回1,到头都没有返回0.

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

判断向量是否为纯单调递增的,也就是第i个元素的值大于第i-1个元素的值,貌似并没有直接的函数进行判断,那就进行遍历,如果不满足就直接返回值并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

第十七题 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.

返回向量中所有大于锤石,不,阈值thresh的所有元素的下标。

直接使用find函数即可。

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 ]

给定n,返回一个n*n列的矩阵,值按照先列后行从左到右从上到下的顺序从1递增到n^2.

可以分别创建值为1:n的列向量和行向量,然后将他们进行乘积即可得到。

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

1:n直接创建的是行向量,再用‘进行转职即可得到列向量。

第十九题 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)
  y = [x(1) x(end)];
end

测试样例中给出输入'a'的返回值为'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

统计字符串中’1‘出现的次数,由x=='1'可以得到每一个字符是否等于’1‘,等于为1,不等于为0,然后sum得到等于’1‘的数量,或者统计find结果的大小也可以。

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;

考察随机数,随机产生两个1到6的值。使用rendi([m,n],[a,b])得到一个值为a到b(都包括)的m*n大小的 随机数矩阵。

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!

对1到n进行随机排序,自己写起来比较麻烦,直接使用randperm函数。

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)是生成一个n*n的魔方矩阵,他的每行、列、对角线的值都相等,并等于(1+2+3+4+n^2)/n,如果不利用magic的特性,那么对行、列对角线任意求和即可:

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

或者按照特征直接求解,得到m=n(n^2+1)/2.

第二十四题 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.

给定n,求1、2、3、4、…….2^n之和,用等比差列求和公式求即可:

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

 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值