Changing Function number every loop

5 次查看(过去 30 天)
I am trying to figure out what is the best way to change a value n every time I use this function in another loop. I would like n to be 4, then 8, then 12, and then 16. n = [4; 8; 12; 16] I have this specified on another main script using this function. It is being looped 4 times using each of these values. Thank you!
function outpt=TargetRand(n, search, target)
%n=number of x's and o's you want plotted;
%search='c' or 'f'; 'c' means conjunctive search, 'f' means feature search
%target= 'y' or 'n' for presence of target in image;
%h=figure;
%pause
%h725=get(h,'CurrentCharacter');
x=rand(n, 1);
y=rand(n,1);
z=rand(2,1);
if(z(1) <= 0.5)
target_sym='X';
not_target='O';
else
target_sym='O';
not_target='X';
end
if(z(2) <= 0.5)
target_col='r';
not_col='g';
else
target_col='g';
not_col='r';
end
if(strcmp(target, 'y') && strcmp(search, 'f'))
g=text(x(1), y(1), target_sym);
set(g, 'color', target_col);
for i=2:n
g=text(x(i), y(i), not_target);
set(g, 'color', target_col);
end
elseif(strcmp(target, 'n') && strcmp(search, 'f'))
for i=1:n
if(i<=n/2)
char='X';
else
char='O';
end
g=text(x(i), y(i), char);
set(g, 'color', target_col);
end
elseif(strcmp(target, 'y') && strcmp(search, 'c'))
if(n==4)
g=text(x(1), y(1), target_sym);
set(g, 'color', target_col);
for i=2:4
g=text(x(i), y(i), not_target);
set(g, 'color', not_col);
end
else
z=rand(1,1);
if(z <= 0.5)
unbal_feature='shape';
else
unbal_feature='color';
end
if(strcmp(unbal_feature, 'shape'))
g=text(x(1), y(1), target_sym);
set(g, 'color', target_col);
for i=2:(n/4+1)
g=text(x(i), y(i), target_sym);
set(g, 'color', not_col);
end
for i=(n/4+2):3*n/4
g=text(x(i), y(i), not_target);
set(g, 'color', target_col);
end
for i=(3*n/4+1):n
g=text(x(i), y(i), not_target);
set(g, 'color', not_col);
end
else
g=text(x(1), y(1), target_sym);
set(g, 'color', target_col);
for i=2:(n/4+1)
g=text(x(i), y(i), not_target);
set(g, 'color', not_col);
end
for i=(n/4+2):3*n/4
g=text(x(i), y(i), target_sym);
set(g, 'color', not_col);
end
for i=(3*n/4+1):n
g=text(x(i), y(i), not_target);
set(g, 'color', target_col);
end
end
end
else
if(n==4)
for i=1:2
g=text(x(i), y(i), target_sym);
set(g, 'color', target_col);
end
for i=3:4
g=text(x(i), y(i), not_target);
set(g, 'color', not_col);
end
else
for i=1:(n/4)
g=text(x(i), y(i), target_sym);
set(g, 'color', target_col);
end
for i=(n/4+1):(n/2)
g=text(x(i), y(i), not_target);
set(g, 'color', not_col);
end
for i=(n/2+1):(3*n/4)
g=text(x(i), y(i), not_target);
set(g, 'color', target_col);
end
for i=(3*n/4+1):n
g=text(x(i), y(i), target_sym);
set(g, 'color', not_col);
end
end
end
outpt=1;

采纳的回答

dpb
dpb 2022-9-10
Seems would be just call it in a loop or even with arrayfun...
search='c';
target='y';
N=[4:4:16].';
ret=arrayfun(@(n)TargetRand(n,search,target),N);
for a fixed set of the other parameters buried inside the anonymous function.
If they also vary by iteration, then pass them, too, or use an explicit for...end loop.

更多回答(0 个)

类别

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