Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
矢量化MATLAB代码_Matlab_Vectorization - Fatal编程技术网

矢量化MATLAB代码

矢量化MATLAB代码,matlab,vectorization,Matlab,Vectorization,假设我们有三个大小相等的m×n矩阵:A,B,C C中的每一列都代表一个时间序列。 A是C中每个时间序列的运行最大值(在固定窗口长度上)。 B是C中每个时间序列的运行最小值(在固定窗口长度上) 有没有办法以矢量化的方式确定T [nrows, ncols] = size(A); T = zeros(nrows, ncols); for row = 2:nrows %loop over the rows (except row #1). fo

假设我们有三个大小相等的m×n矩阵:
A
B
C

C
中的每一列都代表一个时间序列。
A
C
中每个时间序列的运行最大值(在固定窗口长度上)。
B
C
中每个时间序列的运行最小值(在固定窗口长度上)

有没有办法以矢量化的方式确定
T

[nrows, ncols] = size(A);
T = zeros(nrows, ncols);
for row = 2:nrows                           %loop over the rows (except row #1).
    for col = 1:ncols                       %loop over the columns.
        if     C(row, col) > A(row-1, col)
            T(row, col) =  1;
        elseif C(row, col) < B(row-1, col)
            T(row, col) = -1;
        else
            T(row, col) = T(row-1, col);
        end
    end
end
[nrows,ncols]=大小(A);
T=零(nrows,NCOL);
对于行=2:nrows%在行上循环(除了行#1)。
对于col=1:ncols%在列上循环。
如果C(行,列)>A(行-1,列)
T(行,列)=1;
其他C(行,列)
这就是我到目前为止的想法:

T = zeros(m, n);
T(C > circshift(A,1)) =  1;
T(C < circshift(B,1)) = -1;
T=0(m,n);
T(C>circshift(A,1))=1;
T(C
进行矢量化非常困难,因为当前行的输出取决于前一行的输出。进行矢量化操作通常意味着每个元素都应该使用独立于其周围的其他元素的关系独立于自身

我没有任何关于如何在没有
for
循环的情况下实现这一点的信息,但我可以帮助您将操作减少到一个而不是两个。你可以做每行矢量化的作业,但我看不出你是如何一次完成所有作业的

因此,请尝试以下方法:

[nrows, ncols] = size(A);
T = zeros(nrows, ncols);
for row = 2:nrows                           
    out = T(row-1,:); %// Change - Make a copy of the previous row
    out(C(row,:) > A(row-1,:)) = 1; %// Set those elements of C 
                                    %// in the current row that are larger
                                    %// than the previous row of A to 1
    out(C(row,:) < B(row-1,:)) = -1; %// Same logic but for B now and it's 
                                     %// less than and the value is -1 instead
    T(row,:) = out; %// Assign to the output
end
[nrows,ncols]=大小(A);
T=零(nrows,NCOL);
对于行=2:nrows
out=T(第1行:);%//更改-复制上一行
out(C(row,:)>A(row-1,:)=1;%//将这些元素设置为C
%//在当前行中,较大的
%//比上一行的A到1
out(C(row,:)

我目前正在研究如何使用任何循环来实现这一点。我会随时通知您。

实现矢量化相当困难,因为当前行的输出取决于前一行的输出。进行矢量化操作通常意味着每个元素都应该使用独立于其周围的其他元素的关系独立于自身

我没有任何关于如何在没有
for
循环的情况下实现这一点的信息,但我可以帮助您将操作减少到一个而不是两个。你可以做每行矢量化的作业,但我看不出你是如何一次完成所有作业的

因此,请尝试以下方法:

[nrows, ncols] = size(A);
T = zeros(nrows, ncols);
for row = 2:nrows                           
    out = T(row-1,:); %// Change - Make a copy of the previous row
    out(C(row,:) > A(row-1,:)) = 1; %// Set those elements of C 
                                    %// in the current row that are larger
                                    %// than the previous row of A to 1
    out(C(row,:) < B(row-1,:)) = -1; %// Same logic but for B now and it's 
                                     %// less than and the value is -1 instead
    T(row,:) = out; %// Assign to the output
end
[nrows,ncols]=大小(A);
T=零(nrows,NCOL);
对于行=2:nrows
out=T(第1行:);%//更改-复制上一行
out(C(row,:)>A(row-1,:)=1;%//将这些元素设置为C
%//在当前行中,较大的
%//比上一行的A到1
out(C(row,:)

我目前正在研究如何使用任何循环来实现这一点。我会随时通知你的。

好吧,问题是条件语句的ELSE部分存在依赖性。所以,经过长时间的脑力锻炼,我总结出了一种方法,可以将所有东西矢量化

现在,这种方法是基于映射的。我们为
ELSE
部件获取与
2D
掩码相对应的
1s
列运行或孤岛,并为它们分配相同的标记。然后,我们沿着每次运行的每列转到
start-1
,并存储该值。最后,用这些标记的数字对每个
start-1
进行索引,这将作为映射索引,为我们提供在新输出中设置的所有元素

以下是实现所有这些愿望的实施方案-

%// Store sizes
[m1,n1] = size(A);

%// Masks corresponding to three conditions
mask1 = C(2:nrows,:) > A(1:nrows-1,:);
mask2 = C(2:nrows,:) < B(1:nrows-1,:);
mask3 = ~(mask1 | mask2);

%// All but mask3 set values as output
out = [zeros(1,n1) ; mask1 + (-1*(~mask1 & mask2))];

%// Proceed if any element in mask3 is set
if any(mask3(:))

    %// Row vectors for appending onto matrices for matching up sizes
    mask_appd = false(1,n1);
    row_appd = zeros(1,n1);

    %// Get 2D mapped indices
    df = diff([mask_appd ; mask3],[],1)==1;
    cdf = cumsum(df,1);

    offset = cumsum([0 max(cdf(:,1:end-1),[],1)]);
    map_idx = bsxfun(@plus,cdf,offset);
    map_idx(map_idx==0) = 1;

    %// Extract the values to be used for setting into new places
    A1 = out([df ; false(1,n1)]);

    %// Map with the indices obtained earlier and set at places from mask3
    newval = [row_appd ; A1(map_idx)];
    mask3_appd = [mask_appd ; mask3];
    out(mask3_appd) = newval(mask3_appd);

end
%//存储大小
[m1,n1]=尺寸(A);
%//对应于三个条件的掩码
mask1=C(2:nrows,:)>A(1:nrows-1,:);
mask2=C(2:nrows,:)
问题是,条件语句的ELSE部分存在依赖关系。所以,经过长时间的脑力锻炼,我总结出了一种方法,可以将所有东西矢量化

现在,这种方法是基于映射的。我们为
ELSE
部件获取与
2D
掩码相对应的
1s
列运行或孤岛,并为它们分配相同的标记。然后,我们沿着每次运行的每列转到
start-1
,并存储该值。最后,用这些标记的数字索引到每一个这样的
start-1
,这将作为映射索引,为我们提供一个