How do I loop vector calculations and store them in another vector?

1 view (last 30 days)
I have a list of two sets of vectors with names ranging x1 to x102 and y1 to y 102. I want to calculate the sum count from the y vector in the corresponding bins in the x vector over all 102 and store them in one vector with 102 bins.
How do I do this?
This is the code I wish to iterate over all 102 x'num' and y'num' vectors. Remember, x1 is a vector itself and the number stored in the ith bin in the z vector sums the y values in the corresponing bins in the x vector. I imagine looks like the following
iterate from i=1 to 102
z(i)= sum(yi(find(xi >= 1495 & xi <= 1505)))
end

Answers (1)

Adam
Adam on 5 Sep 2014
Edited: Adam on 5 Sep 2014
Store your data in two 2d matrices x and y instead of 204 individually named variables, then it is just a simple indexing case. i.e. x is a (102 * n) matrix where n is whatever the length is of your x vectors. If each of your x vectors can be different length then it is probably simplest to use a cell array although a 2d matrix of a size large enough to hold the longest vector would also work.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!