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
Click For Summary

Discussion Overview

The discussion revolves around creating a 'for' loop in MATLAB to process a 5000x1 vector in groups of 11 values. Participants explore how to implement the loop effectively to calculate results for each set of values, addressing potential issues with function definitions and data types.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant seeks guidance on how to structure a 'for' loop to process a vector in segments of 11 values.
  • Another participant suggests using two nested 'for' loops to handle summation or other operations on groups of 11 elements.
  • A proposed code snippet demonstrates how to access groups of 11 elements using MATLAB's indexing, noting that the last group may be incomplete due to the vector's length not being divisible by 11.
  • Concerns are raised about an error message related to an undefined function, with suggestions to check input types and ensure consistent output sizes in the function.
  • Participants discuss the need for debugging the function with simpler inputs and ensuring that calculations do not lead to zero values that could affect the results.
  • There are comments on the clarity of the code and suggestions for best practices in function definition and variable assignment.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to implement the loop and handle potential errors. There is no consensus on the optimal solution, and various methods are proposed and critiqued.

Contextual Notes

Limitations include potential issues with the size of input matrices and the handling of edge cases where the vector length is not a multiple of 11. There are also unresolved questions about the function's output consistency and the handling of specific calculations.

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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
Replies
3
Views
1K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 28 ·
Replies
28
Views
3K