HELP I'm a complete beginner! Is it possible to create a loop within a function?

1 view (last 30 days)
Hi! I'm a complete MATLAB beginner and have been stuck with this problem for the past 48 hours.
I have a variable "templates" which is an array 58x2x240 - it basically consists of 58 X and Y coordinates for face landmark locations of 240 faces - both male and female.
I have a second variable "sex" whis is a vector (or matrix??) 1x240 - it basically contains the numbers 0 and 1 - 0 for male face, 1 for female face. It corresponds to the faces in the "templates" array.
I'm supposed to make a function to average the templates for the male faces. So basically get a 58x2 matrix that has the average X and Y coordinates of the 120 male faces. My problem at this stage is that I can't figure out how to use the "sex" variable to sort out only the male faces in the "templates" array. Do I have to create a loop? If so, should the loop be inside the function? This is what I've tried:
function[average]=averageFace(templates, sex, gender)
%btw I have no idea what the variable "gender" could be, but the assignment
%says the function should take as input "an array containing all the
%templates; a vector specifying, in order the gender of each template; the
%gender to average over, 0 for males, 1 for females."
for loop = 1:240
image=templates(:,:,loop);
maleFace=Image(gender==0);
end
%there would obviously be more stuff here - I just haven't gotten to that yet
end
My idea was that this should give me an array 58x2x120 of only the rows that correspond to 0 in the "sex" vector. It doesn't seem to be working at all!
If someone could PLEASE help me I would be forever grateful! I have no idea if I'm even on the right track here.
  4 Comments
Jan
Jan on 25 Oct 2012
I've applied code-formatting. Please, Andrea, learn how to format code in this forum.
It seems like the question is re-opened again.
Jan
Jan on 25 Oct 2012
Edited: Jan on 25 Oct 2012
These could be reasons for closing the question:
  • I cannot find a relation between the title "Is it possible to create a loop within a function?" and the question.
  • The question is not really clear, but a discussion would probably result in more clarifications.
  • As long as "code on its own" does not make sense to the author, a discussion in a Matlab forum seems to be inappropriate. I'm convinced that "thinking in words" is not a successful strategy for solving a Matlab course.
And as usual my personal opinion: I detest automatic face recognition, because it let machines control the freedom of human. When I take into account, that my bank is not able to offer something more sophisticated than a 4 digits PIN code to secure my money, I do not want to allow even less public systems to scan my face. Some day I will get an email: "Hi, we have registered that you perform a jogging session each Thursday in the park, but not today. Please contact a physician if you feel ill. Otherwise we have to increase the fees of your health insurance...". But of course such realistic or naive considerations are not a valid reason for closing a thread.

Sign in to comment.

Accepted Answer

Jan
Jan on 25 Oct 2012
Welcome to Matlab and the world of programming!
It will not be useful to explain a program in words. If you are wanted to implement the program in code, stop "thinking in words", because it is neither a sufficient nor an efficient method for programming. Telling a story and implementing an algorithm are different jobs.
I cannot understand the difference between sex and gender in your question. Therefore I stay at sex:
templates = rand(58, 2, 240); % Random test data, arbitrary values
sex = rand(1, 240) > 0.5; % abut 50% ones, rest are zeros
male_templates = templates(:, :, sex==0);
female_templates = templates(:, :, sex==1);
  3 Comments
Jan
Jan on 25 Oct 2012
@Andrea: Please do not take my moaning personally. I had several discussions in Matlab forums about the responsibility of programmers to the effects of the advertized products. 12 year old German boys recognize 90% of the beer brands by the jingle melody of their tv advertisements. And this is not an accident: The advertising industry knows exactly, that the earlier children drink beer, the more likely they will get a favorite brand they drink for many years. Another example: Near to schools there are more posters which advertise cigarets.
And now I imagine a group of 10 year old children walking through a shopping mall. The automatic age and gender recognition of the video survey let start beer and cigaret commercials in the surrounding monitors, because this will increase the total income of the producers and of the advertising industry.
Although I'm serious, when I want to support the health of our children, I admit that it is not an option, that all Matlab programmers apply for jobs at Greenpeace, the UN and amnesty international. All I want to say is: If you have the chance to, think twice about the effects of your programs.
Sorry for this nearly off-topic philosophizing.

Sign in to comment.

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!