Matlab将循环向量化m,在MATLAB中向量化循环

icon1.gif 在MATLAB中向量化循环

我不太确定这是否可行,但是我对MATLAB的理解肯定会更好。

我有一些代码需要向量化,因为这会导致程序中出现很大的瓶颈。它是优化例程的一部分,具有许多可能的配置,包括短期平均值(STA),长期平均值(LTA)和灵敏度(OnSense)。

时间是矢量格式,FL2onSS是主数据(Nx1的两倍),FL2onSSSTA是其STA(NxSTA的两倍),FL2onSSThresh是其阈值(NxLTAxOnSense的两倍)

这个想法是要计算一个红色警报矩阵,该矩阵将是4D的-整个程序其余部分都使用的alarmStatexSTAxLTAxOnSense。

Red = zeros(length(FL2onSS), length(STA), length(LTA), length(OnSense), 'double'); for i=1:length(STA) for j=1:length(LTA) for k=1:length(OnSense) Red(:,i,j,k) = calcRedAlarm(Time, FL2onSS, FL2onSSSTA(:,i), FL2onSSThresh(:,j,k)); end end end 目前,我已经在重复执行此功能,以期提高其速度,但是显然,如果可以对整个向量进行矢量化处理,效果会更好。换句话说,如果有更好的解决方案,则无需保留该功能。

function [Red] = calcRedAlarm(Time, FL2onSS, FL2onSSSTA, FL2onSSThresh) % Calculate Alarms % Alarm triggers when STA > Threshold zeroSize = length(FL2onSS); %Precompose Red = zeros(zeroSize, 1, 'double'); for i=2:zeroSize %Because of time chunks being butted up against each other, alarms can %go off when they shouldn't. To fix this, timeDiff has been %calculated to check if the last date is different to the current by 5 %seconds. If it isn't, don't generate an alarm as there is either a %validity or time gap. timeDiff = etime(Time(i,:), Time(i-1,:)); if FL2onSSSTA(i) > FL2onSSThresh(i) && FL2onSSThresh(i) ~= 0 && timeDiff == 5 %If Short Term Avg is > Threshold, Trigger Red(i) = 1; elseif FL2onSSSTA(i) < FL2onSSThresh(i) && FL2onSSThresh(i) ~= 0 && timeDiff == 5 %If Short Term Avg is < Threshold, Turn off Red(i) = 0; else %Otherwise keep current state Red(i) = Red(i-1); end end end 代码很简单,因此我不再赘述。如果您需要阐明特定线路的功能,请告诉我。

回答:

诀窍是使用repmat和permute 将所有数据转换为同一格式 。那么逻辑就是简单的部分。

我需要一个讨厌的技巧来实现最后一部分(如果没有任何条件成立,请使用最后的结果)。通常,这种逻辑是使用累计来完成的。我必须使用另一个矩阵2. ^ n来确保使用定义的值(这样+ 1,+ 1,-1确实会提供1,1,0)-仅看代码即可:)

%// define size variables for better readability N = length(Time); M = length(STA); O = length(LTA); P = length(OnSense); %// transform the main data to same dimentions (3d matrices) %// note that I flatten FL2onSSThresh to be 2D first, to make things simpler. %// anyway you don't use the fact that its 3D except traversing it. FL2onSSThresh2 = reshape(FL2onSSThresh, [N, O*P]); FL2onSSThresh3 = repmat(FL2onSSThresh2, [1, 1, M]); FL2onSSSTA3 = permute(repmat(FL2onSSSTA, [1, 1, O*P]), [1, 3, 2]); timeDiff = diff(datenum(Time))*24*60*60; timeDiff3 = repmat(timeDiff, [1, O*P, M]); %// we also remove the 1st plain from each of the matrices (the vector equiv of running i=2:zeroSize FL2onSSThresh3 = FL2onSSThresh3(2:end, :, :); FL2onSSSTA3 = FL2onSSSTA3(2:end, :, :); Red3 = zeros(N-1, O*P, M, 'double'); %// now the logic in vector form %// note the chage of && (logical operator) to & (binary operator) Red3((FL2onSSSTA3 > FL2onSSThresh3) & (FL2onSSThresh3 ~= 0) & (timeDiff3 == 5)) = 1; Red3((FL2onSSSTA3 < FL2onSSThresh3) & (FL2onSSThresh3 ~= 0) & (timeDiff3 == 5)) = -1; %// now you have a matrix with +1 where alarm should start, and -1 where it should end. %// add the 0s at the begining Red3 = [zeros(1, O*P, M); Red3]; %// reshape back to the same shape Red2 = reshape(Red3, [N, O, P, M]); Red2 = permute(Red2, [1, 4, 2, 3]); %// and now some nasty trick to convert the start/end data to 1 where alarm is on, and 0 where it is off. Weights = 2.^repmat((1:N)', [1, M, O, P]); %// ' damn SO syntax highlighting. learn MATLAB already! Red = (sign(cumsum(Weights.*Red2))+1)==2; %// and we are done. %// print sum(Red(:)!=OldRed(:)), where OldRed is Red calculated in non vector form to test this.

更多&回答...

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值