필터 지우기
필터 지우기

How to code NESTED CYCLES

조회 수: 1 (최근 30일)
pipor
pipor 2023년 9월 9일
댓글: Image Analyst 2023년 9월 10일
f=[3 6 3 9]
b=[5 8 10 12]
a is a function with parameter f and b!
for l=1:numel(b)
for i=1:numel(f)
a(f(i),b(l))
end
end
but this is a problem:
if b is empty the function won't loop me with "f"
  댓글 수: 1
Matt J
Matt J 2023년 9월 9일
That doesn't seem like a problem.

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

채택된 답변

Star Strider
Star Strider 2023년 9월 9일
I would set the empty array to 1 (so that it iterates one time only) and be done with it —
a = @(x,y) [x y];
f = [3 6 3 9]; % Neither Empty
b = [5 8 10 12];
if isempty(f)
f = NaN;
elseif isempty(b)
b = NaN;
end
for l=1:numel(b)
for i=1:numel(f)
q = a(f(i),b(l))
end
end
q = 1×2
3 5
q = 1×2
6 5
q = 1×2
3 5
q = 1×2
9 5
q = 1×2
3 8
q = 1×2
6 8
q = 1×2
3 8
q = 1×2
9 8
q = 1×2
3 10
q = 1×2
6 10
q = 1×2
3 10
q = 1×2
9 10
q = 1×2
3 12
q = 1×2
6 12
q = 1×2
3 12
q = 1×2
9 12
f = []; % 'f' Empty
b = [5 8 10 12];
if isempty(f)
f = NaN;
elseif isempty(b)
b = NaN;
end
for l=1:numel(b)
for i=1:numel(f)
q = a(f(i),b(l))
end
end
q = 1×2
NaN 5
q = 1×2
NaN 8
q = 1×2
NaN 10
q = 1×2
NaN 12
f = [3 6 3 9]; % 'b' Empty
b = [];
if isempty(f)
f = NaN;
elseif isempty(b)
b = NaN;
end
for l=1:numel(b)
for i=1:numel(f)
q = a(f(i),b(l))
end
end
q = 1×2
3 NaN
q = 1×2
6 NaN
q = 1×2
3 NaN
q = 1×2
9 NaN
The ‘default’ value (here ‘NaN’) can be any value that makes sense in the context of whatever ‘a’ does. If a given argument is added, then set it to the identity element for that operation, so if it is added set it to 0 if the associated vector is empty, if it is multiplied, set it to 1.
.

추가 답변 (2개)

Bruno Luong
Bruno Luong 2023년 9월 9일
Reverse the 2 loops, then it loops on f
f=[3 6 3 9]
f = 1×4
3 6 3 9
b=[]
b = []
for i=1:numel(f)
f(i)
for l=1:numel(b)
a(f(i),b(l))
end
end
ans = 3
ans = 6
ans = 3
ans = 9
  댓글 수: 2
pipor
pipor 2023년 9월 9일
I wrote the code in general but I don't know which array is empty..it can be f or b
Bruno Luong
Bruno Luong 2023년 9월 9일
편집: Bruno Luong 2023년 9월 9일
Split in three loops if you have to
for i=1:numel(f)
for l=1:numel(b)
% ... do something with both f(i) and b(l)
a(f(i),b(l))
end
end
for i=1:numel(f)
% ... do something with f(i) ALONE
end
for l=1:numel(b)
% ... do something with b(l) ALONE
end

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


Image Analyst
Image Analyst 2023년 9월 9일
Check them in advance:
if isempty(b)
% What to do
return;
end
if isempty(f)
% What to do
return;
end
for l=1:numel(b)
for i=1:numel(f)
something = a(f(i),b(l))
end
end
  댓글 수: 2
pipor
pipor 2023년 9월 9일
이동: Dyuman Joshi 2023년 9월 9일
if b=[] it don't execute cycle for
b=[];
f=[3 4 5];
if isempty(b)
% What to do
return;
end
if isempty(f)
% What to do
return;
end
for l=1:numel(b)
for i=1:numel(f)
fprintf("execure cycle \n");
end
end
Image Analyst
Image Analyst 2023년 9월 10일
Correct. Why would you want to continue if one is empty? If you do you'll just get an error. If you can "fix" the situation, do so inside the if -- for example Star set them to nan instead. If you can't fix the situation, you should just exit the code after alerting the user.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by