Creating a 'For' Loop to Calculate Answers for Sets of 11 Values

  • Thread starter Thread starter pizza89
  • Start date Start date
  • Tags Tags
    Loop Sets
Join the discussion
Registration is free. Start your own thread to ask a follow-up.
5 replies · 5K views
pizza89
Messages
3
Reaction score
0
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.
 
Physics news on Phys.org


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...)
 


You want something like this :
Code:
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.
 


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
 


That looks weird, this will be easier to understand:

Code:
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
 


pizza89 said:
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:

G=zeros(numel(c),1);
G= 1+floor(log(z));

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 = 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));

"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.

for k = (1:11:5379)';
You don't need the transpose (') there, but I don't think it matters.