フィルターのクリア

it's possible to velocize this loop?

2 ビュー (過去 30 日間)
piero
piero 2023 年 8 月 18 日
編集済み: Bruno Luong 2023 年 8 月 18 日
orig=Sis(i).dailyprof;
xx=orig;
yy=Sis(i).Ntradess;
x1=find(xx);
last=-1;
for i=numel(x1):-1:1
if yy(x1(i))~=last
last=yy(x1(i));
xx(x1(i))
xx(x1(i))=xx(x1(i))-k;
xx(x1(i))
end
end
class(xx) double
class(YY) double
%%CHECK!
%k=[orig xx yy];
  1 件のコメント
ProblemSolver
ProblemSolver 2023 年 8 月 18 日
編集済み: Walter Roberson 2023 年 8 月 18 日

サインインしてコメントする。

採用された回答

Bruno Luong
Bruno Luong 2023 年 8 月 18 日
編集済み: Bruno Luong 2023 年 8 月 18 日
@piero Your question/code is awfully asked: no comment, no description, cannot run as it is, debug code left over, etc....
load('matlab_orig.mat')
load('matlab_yy.mat')
xx = orig;
k = 100;
x1 = find(xx);
last=-1;
for i=numel(x1):-1:1
if yy(x1(i))~=last
last=yy(x1(i));
xx(x1(i))=xx(x1(i))-k;
end
end
xx2 = orig;
x1 = find(xx2);
yyx1 = yy(x1);
last = x1([diff(yyx1(:))~=0; true]);
xx2(last) = xx2(last) - k;
isequal(xx, xx2)
ans = logical
1
  1 件のコメント
piero
piero 2023 年 8 月 18 日
always top! Thank you

サインインしてコメントする。

その他の回答 (1 件)

ProblemSolver
ProblemSolver 2023 年 8 月 18 日
However, to the understanding what you are looking for is VECTORIZING the loop:
First, you should change 'find to logical indexing:
x1 = logical(orig);
Then, pre-allocate xx and yy to avoid repeated memory allocations:
xx = zeros(size(orig));
yy = zeros(size(orig));
The MAIN part I guess what you are looking for:
last = [0; yy(x1(1:end-1))];
mask = [yy(x1) ~= last];
xx(x1) = orig(x1) - k;
xx(x1(mask)) = xx(x1(mask)) - k;
Now you can use bsxfun to vectorize the difference operation:
mask = [true; diff(yy(x1))~=0];
xx(x1) = orig(x1) - k*double(bsxfun(@times, mask, ones(sum(x1),1)));
For optimization, the class checks should be avoided and just use 'double'
xx = double(zeros(size(orig)));
yy = double(zeros(size(orig)));
  1 件のコメント
piero
piero 2023 年 8 月 18 日
thanks but it give me an error..
Arrays have incompatible sizes for this operation.
Error in untitled12 (line 30)
mask = [yy(x1) ~= last];
i attachment file

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by