Dynamically Creating Variable Alternatives

조회 수: 1 (최근 30일)
Eric Hoffmann
Eric Hoffmann 2022년 5월 24일
댓글: Eric Hoffmann 2022년 5월 25일
Hi,
Currently I have a 3D matrix of zeros (1000,1000,430) and I need to iterate through slices 1:430. I want each slice done in parallel as each slice can be done independently and I have access to a super computer with many cores and each slice takes around 4800 seconds on my PC. Each slice of zeros will then be given different values in each index defining locations of the brain. Without parfor I can just simply call my 3D preallocated matrix of zeros and redefine different indexes. However, this matrix can't be called and edited by different cores in parallel. This means I need an independent matrix for each slice. Currently, I have preallocated matrixes A1-A430 using the forbidden eval function in a for loop. However, I am unsure how to call these matrixes in a for loop without using the eval function. For example when I am in slice 100 I would need to edit A100. I am unsure how to do this without using eval. On that note I assumed other structures cannot be called in parallel and this is why I stuck with the forbidden dynamic variable creation. Any advice would be greatly appreciated.
  댓글 수: 2
Eric Hoffmann
Eric Hoffmann 2022년 5월 25일
Wow such a useful link. I am confused how I never came across this in my own research. Hiding eval in a function would fix the problem I believe. I will accept the other answer as that has also worked and doesn't require dynamic variables, but this will be the route I will go down if that doesn't work in the end.

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

채택된 답변

Walter Roberson
Walter Roberson 2022년 5월 24일
parfor sliceno=1:size(Data, 3)
slice = Data(:, :, sliceno);
change slice
Data(:, :, sliceno) = slice;
end
  댓글 수: 1
Eric Hoffmann
Eric Hoffmann 2022년 5월 25일
Wow thank you for such a quick response. I was indexing with sliced variables in the wrong manner as you correctly show above. How dumb of me! I also wanted to include the link below as that fixes the next problem that appears once applying the correction above. Thanks again everyone!
https://www.mathworks.com/matlabcentral/answers/817330-how-to-resolve-indexing-the-sliced-variable-error-in-the-case-of-for-nested-with-parfor

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by