is there any way to keep the uninitialized variable in parfor and recall it outside
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
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 Comment
Amirah Algethami
on 29 Apr 2024
Thank you very much I got it @Manikanta Aditya
Accepted Answer
Manikanta Aditya
on 29 Apr 2024
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 Comments
Amirah Algethami
on 29 Apr 2024
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.
Manikanta Aditya
on 30 Apr 2024
Edited: Manikanta Aditya
on 30 Apr 2024
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!
Amirah Algethami
on 30 Apr 2024
Edited: Amirah Algethami
on 30 Apr 2024
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{:});
Amirah Algethami
on 1 May 2024
Thank you definitly you helped me.
Manikanta Aditya
on 1 May 2024
Thanks, Great to hear!
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
See Also
on 29 Apr 2024
on 1 May 2024
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)