# How to condense 'for' loops function ?

조회 수: 2 (최근 30일)
Sura Kijpaiboonwat 2022년 12월 15일
댓글: Stephen23 2023년 3월 9일
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이전 댓글 -1개 표시이전 댓글 -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개)

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이전 댓글 -1개 표시이전 댓글 -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)
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에서 Matrix Indexing에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by