How to condense 'for' loops function ?
7 views (last 30 days)
Show older comments
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 Comment
Stephen23
on 15 Dec 2022
"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.
Answers (1)
Nadia Shaik
on 9 Mar 2023
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 Comment
Stephen23
on 9 Mar 2023
"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
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!