How to condense 'for' loops function ?
조회 수: 7 (최근 30일)
이전 댓글 표시
I wonder if my code can be replace with cellfun, arrayfun, bsxfun or something that more condense and faster computation for the larger project.
The output from this code should have size as
size(output) >>> (100,3,50x150) or (100, 3, 7500)
input_x = rand(100,3,50);
input_y = rand(100,3,150);
output = [];
for idx_axis = 1:3
i=1
for idx_x = 1:size(input_x,3)
for idx_y = 1:size(input_y,3)
% do some function for this 2 variables here
output(:,idx_axis,i) = input_x(:,axis,idx_x) + input_y(:,axis,idx_y)
% size(output) = (100,1,1) + (100,1,1) = expand the output at 3rd dimension (100,1,new)
i=i+1
end
end
end
댓글 수: 1
Stephen23
2022년 12월 15일
"I wonder if my code can be replace with cellfun, arrayfun, bsxfun or something that more condense and faster computation for the larger project."
That depends entirely what "some function for this 2 variables here" you have. Without knowing that, we don't know either.
답변 (1개)
Nadia Shaik
2023년 3월 9일
Hi Sura,
I understand that you wish to replace the nested for loops in your code to make the code more concise and faster.
You can use "bsxfun" function to perform element-wise operations on two arrays with different dimensions. Below is a code snippet for the same:
input_x = rand(100,3,50);
input_y = rand(100,3,150);
% Reshape input_x and input_y to 2D matrices
x = reshape(input_x, size(input_x,1), []);
y = reshape(input_y, size(input_y,1), []);
result = bsxfun(@plus, x, permute(y, [1 3 2]));
% Reshape the result to the desired output shape
output = reshape(result, [], 3, size(input_x,3)*size(input_y,3));
size(output)
The size of the resultant output is (300, 3, 7500). This code should be faster than the nested loops, especially for larger inputs.
I hope this helps!
댓글 수: 1
Stephen23
2023년 3월 9일
"The size of the resultant output is (300, 3, 7500)."
Which is the wrong size. The OP specified that the output should be "(100, 3, 7500)", and that is also what the OP's code returns. Lets check it now:
input_x = rand(100,3,50);
input_y = rand(100,3,150);
output0 = [];
for idx_axis = 1:3
i=1;
for idx_x = 1:size(input_x,3)
for idx_y = 1:size(input_y,3)
% do some function for this 2 variables here
output0(:,idx_axis,i) = input_x(:,idx_axis,idx_x) + input_y(:,idx_axis,idx_y);
% size(output) = (100,1,1) + (100,1,1) = expand the output at 3rd dimension (100,1,new)
i=i+1;
end
end
end
size(output0)
Here is an actually equivalent, simpler calculation using BSXFUN:
output1 = bsxfun(@plus, permute(input_x,[1,2,4,3]), input_y);
output1 = reshape(output1,size(output1,1),size(output1,2),[]);
size(output1)
And finally lets check if that gives exactly the same result as the OP's code:
isequal(output0,output1)
Why did I not put this as an answer?: https://www.mathworks.com/matlabcentral/answers/1878687-how-to-condense-for-loops-function#comment_2519497
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!