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
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
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)
ans = 1×3
300 3 7500
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
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)
ans = 1×3
100 3 7500
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)
ans = 1×3
100 3 7500
And finally lets check if that gives exactly the same result as the OP's code:
isequal(output0,output1)
ans = logical
1

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by