![6fecb25150f271fb31c9053aba6ccce3.png](https://i-blog.csdnimg.cn/blog_migrate/89185012a27b97ec685a11fda25f1820.jpeg)
上一篇文章的最后一个问题,我在所有的解中看到了更好的答案:
function a = checkerboard(n)
f = repmat(eye(2),ceil(n/2)); %ceil函数向最大整数圆整
a = f(1:n,1:n);
end
的确很巧妙。
11.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 ];
首位列互换,先求出列数,再1与n列互换
size函数:
sz = size(A)
szdim = size(A,dim)
[m,n] = size(A)
[sz1,...,szN] = size(A)
function B = swap_ends(A)
n=size(A,2);
B = A;
B(:,[1,n])=B(:,[n,1])
end
12.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
判断是否是从第二项开始每一项都比前一项大的矩阵。