# Homework Help: MATLAB function: simple 'for' loop. How do you only use a specific part of the data?

1. Aug 5, 2011

### pizza89

I have a 5000x1 vector and am trying to write a function to calculate an answer for entry 1-11, then 12-22, then 23-33, etc. ...

I've been trying to use a 'for' loop, basically:

for i = (???????)
x=i+1
end

Not sure what to put in the ???? area.
I want it to spit out answers for each set of 11 values, but can't figure out how.

2. Aug 5, 2011

### DiracRules

Re: MATLAB function: simple 'for' loop. How do you only use a specific part of the da

Probably I have not understood the problem. Correct me if I haven't: you want to sum up (or do other things) the elements of a vector in groups of 11 each.

If this is your question, have you thought about doing this with 2 for loops? One summing the elements of the group, the other changing the group ? That is, in the inner loop you use a counter, say i, to switch between the elements of the vector (from 0 to 10, that is, reading 11 elements), and in the outer loop you have a counter that switches between the group (first elevens, second elevens, third elevens...)

3. Aug 6, 2011

### uart

Re: MATLAB function: simple 'for' loop. How do you only use a specific part of the da

You want something like this :
Code (Text):

x=[1:5000];

for k=[1:11:4990]
x(k:k+10)
end
The x(k:k+10) is a group of 11 consecutive elements (I know it looks like only 10 at first glance, but the matlab sub-range notation includes both the first and last elements).

Note that 5000 is not divisible by 11 so you'll lose the last 6 elements. You'll have to decide what you want to do there.

4. Aug 8, 2011

### pizza89

Re: MATLAB function: simple 'for' loop. How do you only use a specific part of the da

Thanks! I did something like that and ended up getting, "Undefined function 'galarm' for input arguments of type 'double'." Maybe a 'str2double' (or actually 'double2--whatever type of argument it wants'... I'm not very familiar with those types of things)

Here's what I have:

function G=galarm(c)

G=zeros(numel(c),1); % create G, 1 output for each element in c
x = c(1:5379);

for k = (1:11:5379)';
x(k:k+10);

s = x(k+9:k+10); % source counts
b = mean(k:k+7); %background
stdev = std(k:k+7); %standard deviation of background

z = abs(s-b)/stdev;

G= 1+floor(log(z)); % alarm level (1 thru 5) = number of standard deviations above
background

end

5. Aug 8, 2011

### pizza89

Re: MATLAB function: simple 'for' loop. How do you only use a specific part of the da

That looks weird, this will be easier to understand:

Code (Text):
function G=galarm(c)

G=zeros(numel(c),1); % create G, 1 output for each element in c
x = c(1:5379);

for k = (1:11:5379)';
x(k:k+10);

s = x(k+9:k+10); % source counts
b = mean(k:k+7); %background
stdev = std(k:k+7); %standard deviation of background

z = abs(s-b)/stdev;

G= 1+floor(log(z)); % alarm level (1 thru 5) = number of standard deviations above
background

end

6. Aug 8, 2011

### uart

Re: MATLAB function: simple 'for' loop. How do you only use a specific part of the da

I'm not sure about the size of your input matrix "c", but the two "G" definitions look like they may be of conflicting size. Try debugging your function with just a simple vector (1,n) input.
Also, it's good practice to just assign the function output variable once during the function (and use a temporary variable if necessary to achieve this).

"s" is a (two) vector so the returned result here will be a (two) vector. Is that what you want?

You might also want to make sure that neither (s-b) or stdev can be zero.

You don't need the transpose (') there, but I don't think it matters.