Cody Problem 1-10 Solutions

原创 2016年08月29日 10:06:03

Problem 1  Times 2

function y = times2(x)
  % Modify the line below so that the output y is twice the incoming value x
  y = x*2;
  % After you fix the code, press the "Submit" button, and you're on your way.
end


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


function x = oneToTen
  x = [1:1:10];
end


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

function y = vecsum(x)
   y = sum(x);
end

Problem 4. Make a checkerboard matrix

function a = checkerboard(n)
  a = ones(n);
  a(1:2:n, 2:2:n) = 0;
  a(2:2:n, 1:2:n) = 0;
end

Problem 5. Triangle Numbers

function t = triangle(n)
    t = sum([1:n]);
end


Problem 6. Select every other element of a vector

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

Problem 7. Column Removal

function B = column_removal(A,n)
  A(:,n) = [] ;
  B = A;
end

Problem 8. Add two numbers

function c = add_two_numbers(a,b)
  c = a+b;
end


Problem 9. Who Has the Most Change?

function b = most_change(a)
  price = [0.25; 0.05; 0.1; 0.01];
  c = a*price;
  b= find(c==max(c));
end

ps: 每次找index这种题都不是很顺利

Problem 10. Determine whether a vector is monotonically increasing

function tf = mono_increase(x)
  tf = true;
  for i = 1:length(x)-1
      result = x(i+1)>x(i);
      if isequal(result, false)
          tf = false;
      end
  end     
end












版权声明:本文为博主原创文章,未经博主允许不得转载。

Project Euler Solutions(Problem 1~7)

Project Euler 的解答,使用mathematica实现。

[matlab]mathworks上的cody challenge题解及一些常用函数的总结(1)

暑假想练一下matlab,就想像练C++一样找一个可以刷题的OJ。很幸运的是,matlab官网上就有:http://www.mathworks.com/matlabcentral/cody 花了些时间...

算法:Solutions for the Maximum Subsequence Sum Problem

The maximum subarray problem is the task of finding the contiguous subarray within a one-dimension...

Physicists Discover a Whopping 13 New Solutions to Three-Body Problem

转自:http://news.sciencemag.org/sciencenow/2013/03/physicists-discover-a-whopping.html 物理家发现三体问题的13...

10 CSS3 and jQuery Loading Animations Solutions

We are certain that you’ll be surprised to see the amazing animated series as we bring for you these...

2017 Multi-University Training Contest 1 solutions BY BUAA

1001. Add More Zero答案就是 ⌊log10(2m−1)⌋\left \lfloor \log_{10}(2^m - 1) \right \rfloor,注意到不存在 10k=2m10...
  • algzjh
  • algzjh
  • 2017年07月25日 17:57
  • 838

The Python Challenge Level 0-1 Solutions

The Python Challenge 0-1 Solutions前两天听说了有The Python Chanllenge这么一个游戏,于是自己也想来尝试一下。感觉还是一个挺有意思的。附上我在Git...

Sparse solutions with L1 regularization

1. Common solutions to reduce the generalization error are listed as follows : • Collect more...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Cody Problem 1-10 Solutions
举报原因:
原因补充:

(最多只允许输入30个字)