is there any way to keep the uninitialized variable in parfor and recall it outside
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Hi
This is how it look like my code:
b = [];
parfor i = 1:n
for j=1:m
if b && some_condition(i)
t=do_something(i);
b = [b;t];
end
end...
end
I want to save b matrix for every itration , please help
Best
댓글 수: 1
Thank you very much I got it @Manikanta Aditya
채택된 답변
In MATLAB, when using a parfor loop (parallel for loop), you need to understand that each iteration of the loop is executed independently and potentially on different workers. This means that variables like b in your example cannot be directly modified in the manner you're attempting because it breaks the independence of each iteration.
However, you can collect results from each iteration in a way that is compatible with parfor.
n = 10; % Example value for n
m = 5; % Example value for m
% Preallocate a cell array for the results
results = cell(n, 1);
parfor i = 1:n
temp_b = []; % Temporary variable for this iteration
for j = 1:m
if ~isempty(temp_b) && some_condition(i)
t = do_something(i);
temp_b = [temp_b; t];
end
end
results{i} = temp_b; % Store the result of this iteration
end
% Concatenate all the results after the parfor loop
b = vertcat(results{:});
Hope this helps!
댓글 수: 6
Thanks again. I have more clarification :
1-what if the cell(n, 1)not exactly have same size of the loop itration , it may be less than n.
In MATLAB, when using a parfor loop, each iteration of the loop operates independently, and the results from each iteration need to be stored in a way that does not depend on the results of other iterations. This is why a cell array is a good choice for collecting results, as each cell can independently hold the result from each iteration, regardless of size or type. However, your clarification question touches on a scenario where the number of results you want to store may be less than the number of iterations of the loop, due to possibly skipping some iterations based on certain conditions.
If the actual number of results is less than the number of loop iterations (n), you have a few options to handle this situation:
You can still initialize your cell array with n elements (cell(n, 1)) and only fill in the cells for which you have results. After the loop, you can remove the empty cells. This method works well if you don't know in advance how many iterations will produce results.
results = cell(n, 1); % Preallocation
parfor i = 1:n
% Your loop code here
% Only assign to results{i} if you have valid results
end
% Remove empty cells after the loop
results = results(~cellfun('isempty', results));
Another approach is to collect results in a temporary variable within each iteration and then use a parfor-compatible method to concatenate these results. This approach might involve using a more complex structure or additional indexing logic to ensure that results are collected correctly.
Thanks!
Thanks again just one more Q. I will use your example to show you
n = 10; % Example value for n
m = 5; % Example value for m
% Preallocate a cell array for the results
results = cell(n, 1);
parfor i = 1:n
temp_b = []; % Temporary variable for this iteration
for j = 1:m
if ~isempty(temp_b) && some_condition(i)
t = do_something(i);
temp_b = [temp_b; t];
see here is not valide in my code untill I get it out from if condition
end
I mean here is valid .... but Actully I need to collect temp_b from result of condition.. so how to do it
end
results{i} = temp_b; % Store the result of this iteration
end
% Concatenate all the results after the parfor loop
b = vertcat(results{:});
Pease please answer me
Based on your scenario and the issue you're facing with the parfor loop and conditional statement inside it, it seems you want to collect temp_b for each iteration of i when certain conditions are met. The core issue you're encountering is related to how to properly collect and concatenate these results, especially under specific conditions within the nested loop.
Given the constraints and the need to execute conditionally within the parfor loop, let's refine the approach to ensure temp_b is collected correctly based on your condition and then concatenated after the loop.
n = 10; % Example value for n
m = 5; % Example value for m
% Preallocate a cell array for the results
results = cell(n, 1);
parfor i = 1:n
temp_b = []; % Temporary variable for this iteration
for j = 1:m
% Assuming some_condition(i) is your actual condition
% and you want to collect results based on this condition.
if some_condition(i)
t = do_something(i);
% Directly append to temp_b if condition is met
temp_b = [temp_b; t];
end
end
% Store temp_b for this iteration if any condition was met and temp_b was updated
if ~isempty(temp_b)
results{i} = temp_b;
end
end
% Concatenate all the non-empty results after the parfor loop
% Filter out empty cells first
nonEmptyResults = results(~cellfun('isempty', results));
b = vertcat(nonEmptyResults{:});
Thank you definitly you helped me.
Thanks, Great to hear!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
