Nested / parfor loop issue

조회 수: 8 (최근 30일)
Micheal Simpson
Micheal Simpson 2016년 1월 4일
댓글: Brendan Hamm 2016년 1월 5일
I am currently trying to run a loop while concurrently running another loop. However, the two different loops do not match in terms of stepping which is giving me some issues.
For example, let's say I have a matrix January, with dimensions (24,6,31) for 24 hours, 6 columns of data, and 31 days that is empty. I want to put data into this matrix from a master matrix, that is 744 x 6 (744 hours in the month of January with the 6 columns of data).
Now, to simply put the first days worth of data into the January matrix, I have: January(:,:,1) = Master(1:24,1:6) The second day would be: January(:,:,2) = Master(25:48,1:6) etc. etc.
What would be the easiest way to loop through this? I have..
January = zeros(24,6,31); for i = 1:24:744 for j = 1:31 January(:,:,j) = Data(i:i+23,1:6); end end
But, this is obviously not correct. Using the parfor function gives me an error because the step between i and j are not the same. Any help would be greatly appreciated!

채택된 답변

Brendan Hamm
Brendan Hamm 2016년 1월 4일
a) You must be trying to use parfor on the outer i loop, but this does not allow you to make an assignment to the jth page of January as parfor will not allow this.
b) Why do you need a loop? (See vectorized example)
c) Why do you need to do this in parallel? (This is not a large problem, but if you want just move the parfor to the inside loop)
Try to use vectorization:
% Create a matrix to represent your data:
S = (1:744*6)';
S = reshape(S,744,6); % S now has the format you mentioned
% Now we can just do a reshape on the transposed data and then permute:
S2 = reshape(S.',6,24,[]);
S3 = permute(S2,[2 1 3]);
  댓글 수: 2
Micheal Simpson
Micheal Simpson 2016년 1월 4일
Many thanks, this is a much better approach!
However, from my data, I get instances where the data is multiplied by 1.0e+003 (only if there is data in columns 5 and 6). How does one make this.. not happen? The data is is no more than 3 decimal places long (i.e., data > 0.0009)
Brendan Hamm
Brendan Hamm 2016년 1월 5일
I assume you are talking about the display format. If this is the case you should know that the actual data being stored is to double precision, just the view of that data appears in this manner for easier readability.
You can always change the display format.

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

추가 답변 (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