今日cody之练习

这篇博客包含了一系列关于矩阵操作的MATLAB函数实现,包括向量的对称性检查、特定位置填充1的矩阵生成、乘法表的创建、索引查找、单调递增判断以及列删除等。此外,还涉及了交换输入参数和获取向量中特定值的索引。这些函数展示了基本的算法设计和MATLAB编程技巧。
摘要由CSDN通过智能技术生成

Problem 1451. Symmetry of vector

function y = symmetry(x)
  n=length(x);
   for i=1:n
       if x(i)==x(n-i+1)
           y=1;
       else
           y=0;
       end
   end
end

Problem 1430. Create an n-by-n null matrix and fill with ones certain positions

unction a = FillWithOnes(n,mat)
m=size(mat,1);
a = zeros(n);
for i=1:m
    b=mat(i,:);
    c=b(1,1);
    d=b(1,2);
    a(c,d)=1;
end
end

Problem 33. Create times-tables

function m = timestables(x)
for i=1:x
m(i,:)=i:i:i*x;
end
end

Problem 645. Getting the indices from a vector

function out = findIndices(vec, thresh)
out =find(vec>thresh);
end

Problem 10. Determine whether a vector is monotonically increasing

function tf = mono_increase(x)
if isempty(find(diff(x)<=0))
   tf = true;
else
   tf = false; 
end

Problem 838. Check if number exists in vector

function y = existsInVector(a,b)
if any(find(b(:)==a))==1
    y=1;
else
    y=0;
end

Problem 19. Swap the first and last columns

function B = swap_ends(A)
m=size(A,1);
n=size(A,2);
B=zeros(m,n);
for i=1:n     
     if i==1  
            B(:,i)=A(:,end);
     elseif i==n
            B(:,end)=A(:,1);
     else
            B(:,i)=A(:,i);
     end
end
end

Problem 262. Swap the input arguments

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

Problem 7. Column Removal

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

Problem 2015. Length of the hypotenuse

function c = hypotenuse(a,b)
  c = sqrt(a^2+b^2);
end

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值