Changing Function number every loop

2 views (last 30 days)
Austin Bollinger
Austin Bollinger on 10 Sep 2022
Answered: dpb on 10 Sep 2022
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;

Accepted Answer

dpb
dpb on 10 Sep 2022
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.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!