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

  • Thread starter pizza89
  • Start date
  • Tags
    Loop Sets
In summary, the function returns a vector of 1s, 10s, etc. for each group of 11 values. However, it gets an error when trying to output the results for a matrix with zero rows and columns.
  • #1
pizza89
3
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
  • #2


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


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.
 
  • #4


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


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
 
  • #6


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.
 

1. What is a 'for' loop?

A 'for' loop is a programming concept used to repeat a set of instructions a specific number of times. It consists of three parts: an initializer, a condition, and an increment/decrement. The initializer sets the starting point, the condition determines when the loop should stop, and the increment/decrement updates the loop after each iteration.

2. How do I create a 'for' loop?

To create a 'for' loop, you will need to first initialize a variable and set it to the starting value. Then, add a condition that specifies when the loop should stop. Finally, add an increment or decrement statement to update the variable after each iteration. This will create a loop that will continue until the specified condition is met.

3. How can I use a 'for' loop to calculate answers for sets of 11 values?

To use a 'for' loop to calculate answers for sets of 11 values, you will need to first define the values in an array or list. Then, create a 'for' loop that will iterate through each value in the array and perform the desired calculation. Finally, store the results in a new array or print them out after each iteration.

4. Can a 'for' loop be nested?

Yes, a 'for' loop can be nested within another 'for' loop. This means that one loop is placed inside another loop. This is often used when dealing with multi-dimensional arrays or when performing more complex calculations.

5. Are there any alternatives to using a 'for' loop to calculate answers for sets of 11 values?

Yes, there are other types of loops such as 'while' loops and 'do-while' loops that can also be used to calculate answers for sets of 11 values. Additionally, there are built-in functions and libraries that can perform calculations on arrays without the need for a loop. It is important to choose the most efficient and appropriate method for the specific task at hand.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Replies
3
Views
386
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Advanced Physics Homework Help
Replies
28
Views
2K
Replies
1
Views
791
  • Calculus and Beyond Homework Help
Replies
6
Views
891
Back
Top