Matlab Cody——Basics on Vectors题解

0. Matlab Cody 简介

Matlab Cody是一个类似Leetcode的oj网站,网址:https://ww2.mathworks.cn/matlabcentral/cody

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

1
2
3
function y = vecsum(x)
y = sum(x,'all')
end

2. Problem 6. Select every other element of a vector

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

3. Problem 247. Arrange Vector in descending order

1
2
3
function y = desSort(x)
y = sort(x,'descend');
end

4. Problem 135. Inner product of two vectors

1
2
3
function z = your_fcn_name(x,y)
z = dot(x,y);
end

5. Problem 624. Get the length of a given vector

1
2
3
function y = VectorLength(x)
y = length(x);
end

6. Problem 1107. Find max

1
2
3
function y = your_fcn_name(x)
y = max(x,[],'all');
end

7. Problem 605. Whether the input is vector?

1
2
3
4
5
6
7
function y = checkvector(x)
if isvector(x)
y = 1;
else
y = 0;
end
end

8. Problem 2631. Flip the vector from right to left

题目要求不能直接使用函数,所以自己写

1
2
3
function y = flip_vector(x)
y = x(end:-1:1);
end

9. Problem 3076. Create a vector

1
2
3
function y = zeroToNby2(n)
y = 0:2:n;
end

10. Problem 1024. Doubling elements in a vector

1
2
3
4
5
function B = your_fcn_name(A)
B = zeros(1,2*length(A));
B(1:2:end-1) = A;
B(2:2:end) = A;
end

11. Problem 42651. Vector creation

1
2
3
function y = vector(x)
y = 1:x;
end
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022-2024 CPY
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信