matlab循环矢量化 嵌套,如何在Matlab中对此for循环进行矢量化?

这是构造矩阵的完全向量化方法 - 没有循环,没有arrayfun:

PPR=[0 2 3 5 6 8];

AI=[ 0 0.7854 0.5236 0.3142 0.2618 0.1963];

M = ones(length(PPR),PPR(end)+1); #% allocate proper sized matrix of ones

r=1:length(PPR)-1; #% row indices for 1 element past the end of each row vector

c=PPR(1:end-1)+2; #% corresponding column indices

linear_index = sub2ind(size(M),r,c); #% create linear index from r,c

M(linear_index)=nan; #% set those elements to NaN

M2 = cumsum(M,2)-1; #% use cumsum to propagate the NaN values

M3 = bsxfun(@times,M2,AI'); #%'#multiply each row by the appropriate AI value

xp = sin(M3)+1 #% take the sine of the matrix为清晰起见,我使用了一堆临时变量。如果您想避免使工作区混乱,可以避免使用它们,因为它们通常不会被多次使用。

另请注意:这会在没有指定任何其他值的情况下使用NaN填充矩阵。如果您希望将这些替换为其他默认值(例如零或一些),这些默认值在最后非常简单。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值