Averaging Two Sets of Data with Different Lengths in Matlab

In summary: Yes, I want to average the Rxx's and the Rxy's.OK...Looking at the Minus.tex file, you have just as many Rxymn entries as Rxxmn entries. 443 each. The formatting is bad so that it looks like there are holes in the Rxxmn column of data that are due to mis-tabbing. Is this what you are talking about?The Rxypl and Rxxpl also have 443 entries apiece.
  • #1
mathman44
207
0
Hi,

I have 2 sets of data, one is 472 data points long, the other 370. They are both a function of the same variable "x", they both have the same value for "x" as the last point and the same value for "x" as the first point.

I'm being asked to average the two sets of data, but obviously I can't just use "(y+y2)/2". I also can't cut the 472 data set to 370 and then average because the scale is off. What can I do? Can I have MATLAB average values only when they correspond to the same value of "x"?

Any help would be greatly appreciated. Thanks.
 
Physics news on Phys.org
  • #2
What can of value is x? Scalar or vector?
 
  • #3
Topher925 said:
What can of value is x? Scalar or vector?

Both x and y are scalar.
 
  • #4
mathman44 said:
What can I do? Can I have MATLAB average values only when they correspond to the same value of "x"?
What about a weighted mean?

Can I have MATLAB average values only when they correspond to the same value of "x"?
Yeah, you just take a subset of the 472 set when the x is equal to the x in the 370 set.
something like s1=a(a[1]==b[1])
 
Last edited:
  • #5
Well if they scalars then just sum all the values and divide by x1 + x2.
 
  • #6
"I'm being asked to average the two sets of data..."

Are you sure you understood what you are asked to do? This could mean several things.
 
  • #7
story645 said:
Yeah, you just take a subset of the 472 set when the x is equal to the x in the 370 set.
something like s1=a(a[1]==b[1])

This would work perfectly, do you happen to know the exact command?
 
  • #8
story645 said:
Yeah, you just take a subset of the 472 set when the x is equal to the x in the 370 set.

GCD[472-1,370-1]==3. The x values are identical at exactly 4 points. This would entail ignoring most of the data.
 
  • #9
The x values are identical at many (75% + ball-parking it) points...
 
  • #10
If I understand you correctly, you have two data sets that are functions of x but don't necessarily contain the same x values. A rigorous analysis is a parametric fit, that is, a best fit to a function, typically found by minimizing the least-squared error. Maximum likelihood methods are also common. For example, if y depends linearly on x then a linear regression to all of the data points gives the best fit values of slope and intercept. The best estimate of y at any given x is then easily calculated. Other common functionals are polynomials, exponentials, etc. Non-linear functions are more complicated to fit, of course.
 
  • #11
The x values are identical at exactly 4 points and some close matches.

In any case, one interpretation of the problem encoded in Mathematica is

( (Sum [ y1, {i, 472} ] /472.) + (Sum [ y2, {i, 370} ]/370.) )/2.
 
  • #12
Phrak said:
The x values are identical at exactly 4 points and some close matches.

In any case, one interpretation of the problem encoded in Mathematica is

( (Sum [ y1, {i, 472} ] /472.) + (Sum [ y2, {i, 370} ]/370.) )/2.


They are non-linear sets of data. And why are the x values identical at only 4 points? Look at the sets of data, attached. A is the magnetic field. The prof suggested that we do "Rxxmn + Rxxpl"/2, but it's evident he didn't realize the sets are different sizes...
 

Attachments

  • Minus.TXT
    11.1 KB · Views: 477
  • Plus.TXT
    9 KB · Views: 463
  • #13
Good; it helps to know you are doing experimental physics, rather than an applied
math abstraction. I'm still trying to interpret your text files, n stuff.
 
Last edited:
  • #14
Each file has 443 lines of data. A seems to be your independent variable (or intends to be somewhat independent, abit instrument noise maybe) ranging from -0.02 to 4.40 incremented in units of 0.01.

Where do you get the 472 and 370 counts?
Are the rxx's and rxy's your independent data for which you wish to find averages?
 
  • #15
Phrak said:
Are the rxx's and rxy's your independent data for which you wish to find averages?

Yes, I want to average the Rxx's and the Rxy's. The data was supplied by my prof.
 
Last edited:
  • #16
OK... Looking at the Minus.tex file, you have just as many Rxymn entries as Rxxmn entries. 443 each. The formatting is bad so that it looks like there are holes in the Rxxmn column of data that are due to mis-tabbing. Is this what you are talking about? The Rxypl and Rxxpl also have 443 entries apiece.
 
Last edited:
  • #17
? I see 371 entries for the Rxypl/Rxxpl.
 
Last edited:
  • #18
I see that now. The danged data is full of gaps.
 

1. How can I average two sets of data with different lengths in Matlab?

In Matlab, you can use the mean function to average two sets of data with different lengths. First, you will need to create a new array with the combined length of the two sets. Then, you can use the mean function on this new array to calculate the average.

2. What happens if the two sets of data have different lengths?

If the two sets of data have different lengths, Matlab will automatically ignore any extra values in the longer set and only calculate the average based on the shared values between the two sets.

3. Can I average more than two sets of data with different lengths?

Yes, you can average more than two sets of data with different lengths in Matlab. The mean function can take in multiple arrays as input and will calculate the average based on the shared values between all arrays.

4. Is there a way to specify which values to average in each set of data?

Yes, you can use the nanmean function in Matlab to specify which values to average in each set of data. This function allows you to specify a range of values or indices to be included in the average calculation.

5. Can I calculate the average of two sets of data with different lengths without creating a new array?

Yes, you can use the mean function with the 'omitnan' flag in Matlab to calculate the average of two sets of data with different lengths without creating a new array. This flag tells Matlab to ignore any NaN (not a number) values in the calculation.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
822
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
803
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
851
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
135
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
Back
Top