Hello, I want to use temporary variable value after parfor loop!, how can this achieved??

조회 수: 3 (최근 30일)
counter =0;
parfor i = 1:1000
max=999;
if max < i
max = i;
disp(max);
assignin('base','counter',max);
end
end
disp(counter);
For example, I want to keep max value but I got zero
  댓글 수: 2
Jan
Jan 2019년 4월 29일
This cannot work: 1. you assign the value to the variable counter in the base-workspace, but where does the posted code occur? Inside a function or a script? 2. The order of iterations is undefined in a parfor loop, but the final value depends on the last processed iteration. As consequence your code is equivalent to: randi([1,1000], 1). What do you want to achieve?
saddam alqasimi
saddam alqasimi 2019년 4월 29일
Thank you Mr, Jan,
Honestly, my code is different. I wrote this example to know the solution that would be given, so that I can apply the same solution with mine.
The code want to apply parfor with is a "script", I have a for loop and I wante to change it to parfor loop. One of the errors I can't deal with was the counter because it's used with other struct variables to assigne them. Also the counter is still needed after the loop.
here is the part of the code, I added the variables initializationlines and the parameters just in case you need to know the type.
%---------- parameters
radius = 0.01;
decay = 500;
fade = 1/decay;
minThreshold = 4
%----------- initialization
cmcs.Centre = [];
cmcs.Count = [];
cmcs.Life = [];
cmcs.pointsIndices = [];
outliers = cmcs;
cmcsGraph = graph();
%------------ The loop
for i = 1:sampleSpeed % 1:100
if counter<size(dataset,1)
counter = counter+1;
dataPoint = dataset(counter,1:end-1);
[cmcs,cmcsGraph,outliers] = ...
Assign(cmcs,outliers,dataPoint,radius,minThreshold,cmcsGraph,counter);
end
end
Is it possiable, or what Im trying to do is not right?

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

채택된 답변

Walter Roberson
Walter Roberson 2019년 4월 29일
Create cmsc as a struct array of full size. Assign to cmsc(i) as appropriate. Also keep a logical vector used(i) =true where the condition is met. After the parfor cmsc = cmsc(used)
That graph might be more of a problem. Your code structure tends to hint that you are using information from previous iterations in updating the graph.
  댓글 수: 5
Walter Roberson
Walter Roberson 2019년 5월 16일
You are calling Assign() and Kill() to update cmcs in ways that are not obvious. As outside observers, we would have to assume that the results are dependent upon the order of iterations.
saddam alqasimi
saddam alqasimi 2019년 5월 17일
Yeah, It depends on the iterations but I think it doesn't have to be ordered by iterations as much as it should go through all data-points in the dataset.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by