How can i do it fast way ?

조회 수: 2 (최근 30일)
mankang
mankang 2021년 2월 18일
편집: mankang 2021년 2월 19일
example = mat2cell(rand(100000,4),ones(1,100)*1000,4);
result = cell( size(example, 1), 1);
var1_list = [0.5, 0.7];
var2_list = [0.3, 0.5, 0.7];
final = cell( length(var1_list)*length(var2_list), 1);
cnt = 1;
for var1 = var1_list
for var2 = var2_list
for i = 1 : size(example, 1)
index1 = find( (example{i, 1}(:, 2) >= var1 )==1);
index2 = find( (example{i, 1}(:, 3) >= var2 )==1);
index = intersect(index1, index2);
if ~isempty(index)
result{i, 1} = index;
end
end
final{cnt, 1} = result;
cnt = cnt + 1;
end
end
How can i do it fast way ? or How can i convert to gpuarray?
  댓글 수: 1
Jan
Jan 2021년 2월 18일
Ary you really sure, that you do not want to update result{i,1}, if no intersection was found?

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2021년 2월 18일
편집: Jan 2021년 2월 18일
3 times faster: Replace
index1 = find( (example{i, 1}(:, 2) >= var1 )==1);
index2 = find( (example{i, 1}(:, 3) >= var2 )==1);
index = intersect(index1, index2);
if ~isempty(index)
result{i, 1} = index;
end
by:
index1 = find(all(example{i, 1}(:, 2:3) >= [var1, var2], 2));
if ~isempty(index1)
result{i, 1} = index1;
end
And a further duplication of the speed:
index1 = (example{i, 1}(:, 2) >= var1 & ...
example{i, 1}(:, 3) >= var2);
if any(index1)
result{i, 1} = find(index1);
end
I'd expect something like
if any(index)
result{i, 1} = find(index1);
else % Reset value from former iteration?!
result{i, 1} = [];
end
but maybe this behavior is wanted. In the random tests data, this situation never happens. But if you want to set reslut{i} to [], this is faster than the IF-method above:
index1 = (example{i, 1}(:, 2) >= var1 & ...
example{i, 1}(:, 3) >= var2);
result{i} = find(index1); % Note: {i} is the same as {i, 1}
  댓글 수: 1
mankang
mankang 2021년 2월 18일
편집: mankang 2021년 2월 19일
I'm so happy to finally get your reply.
I saw you a lot in other people's questions.
This way is the fastest. Thanks. bro~
index1 = (example{i, 1}(:, 2) >= var1 & ...
example{i, 1}(:, 3) >= var2);
if any(index1)
result{i, 1} = find(index1);
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Power and Energy Systems에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by