필터 지우기
필터 지우기

how use c++ in MATLAB to run 'for' loops

조회 수: 3 (최근 30일)
mary khaliji
mary khaliji 2015년 8월 9일
댓글: Walter Roberson 2015년 8월 9일
Hello every body. I have a nested for loop that get long time to process, so I want to run the loop on my matrix by C++ and after execution,use from result. How I can do that?
for k=1:length(total_frames)
for j=1:length(all_SVs_in_template)
current_frame2(current_frame2==all_SVs_in_template(j))=l;
end
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2015년 8월 9일
Why are you doing the same test over and over again? Your nested loop does not involve k so the results are going to be the same each time.

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

답변 (1개)

Walter Roberson
Walter Roberson 2015년 8월 9일
Have you considered using
for k=1:length(total_frames)
current_frame2(ismember(current_frame2,all_SVs_in_template))=l;
end
  댓글 수: 3
Image Analyst
Image Analyst 2015년 8월 9일
You probably need the coder or compiler product to convert that MATLAB code into C code or a DLL. Or just make it in C++ directly using Visual Studio.
Walter Roberson
Walter Roberson 2015년 8월 9일
In your code, each element of current_frame2 that is equal to all_SVs_in_template(1) is set to l. Then each element of current_frame2 that is equal to all_SVs_in_template(2) is set to the same value, l. Then each element of current_frame2 that is equal to all_SVs_in_template(3) is set to the same value, l.
A few seconds thought shows us that each element of current_frame2 that is equal to any value in all_SVs_in_template is set to the same value, l.
A way to test whether a value is equal to any of a list of values is to use ismember(). For example,
ismember(17, [8 17 3 4 3 11])
returns true because the initial value 17 appears somewhere in the list [8 17 3 4 3 11]. And your code only cares that each value in current_frame2 is somewhere in the list all_SVs_in_template in order to know that it should set the value to l.
ismember() also works with arrays in the first parameter, and returns a logical array of the same size, true where the particular element appears somewhere in the list that is the second element. So with simple logical indexing,
current_frame2(ismember(current_frame2,all_SVs_in_template))=l;
does everything at the same time.
And then your "for k" loop causes the same tests to be done length(total_frames) times, with exactly the same result each time, since nothing in the inner loop depended upon "k".
If your "l" in your inner loop is intended to be "k" then whether the extra loops are useful depends upon whether the values 1 : length(total_frames) appear in the values in all_SVs_in_template: in that situation, the result would be the same as executing once with k being the single largest value in 1 : length(total_frames) that appears in all_SVs_in_template.

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

Community Treasure Hunt

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

Start Hunting!

Translated by