Hello,
I am trying to convert a program to Python (using numpy and pandas as well) from MATLAB. The input is a (n by 2) array called scaletestmatrix (in this case a 6x2 array). Column one is the x values, which contains some values more than once. Column two is the y values. I need to find the largest and smallest y values that corresponds to each x value, then calculate the difference between the two y values for each x value, and finally find the mean of the differences.
The MATLAB code looks like this:
meanthickness = (mean(thickness))%%% Finds the average of the thickness array
The input array (scaletestmatrix.csv) is:
This is what I have before the For Loop in Python:
This is where I am stuck. Thank you in advance for your help.
I am trying to convert a program to Python (using numpy and pandas as well) from MATLAB. The input is a (n by 2) array called scaletestmatrix (in this case a 6x2 array). Column one is the x values, which contains some values more than once. Column two is the y values. I need to find the largest and smallest y values that corresponds to each x value, then calculate the difference between the two y values for each x value, and finally find the mean of the differences.
The MATLAB code looks like this:
Output:[hr]
data = importdata('C:\\Users\\...\\scaletestmatrix.csv');
xvalues = data(:,1); %%% Extract column 1 of the data as xvalues
yvalues = data(:,2); %%% Extract column 2 of the data as yvalues
smallest = min(xvalues); %%% Finds the smallest xvalue
biggest = max(xvalues); %%% Finds the largest xvalue
length = (biggest - smallest + 1); %%% Finds number of xvalues to evaluate in the impending For Loop
startvalue = smallest %%% Set the starting x value of the impending For Loop
%%% During each loop we find the indices for all of the xvalues equal to startvalue (which increases by 1 each loop).
%%% Then we find the minimum and maximum yvalue that corresponds to the set of indices obtained during that loop.
%%% We then assign those values to finaly1values(y) and finaly2values(y) where y is the index specified by the loop number.
for y=1:length %%% Set the number of loops to run
finaly1values(y) = min(yvalues(xvalues == startvalue))
finaly2values(y) = max(yvalues(xvalues == startvalue))
startvalue = (startvalue +1);
end
thickness = (finaly2values - finaly1values) %%% Creates a 1D array of the difference between the largest and smallest yvalues that correspond to each xvaluemeanthickness = (mean(thickness))%%% Finds the average of the thickness array
The input array (scaletestmatrix.csv) is:
Output:5, 20,
5, 30,
7, 25,
7, 15,
7, 30,
9, 50,
9, 20,
9, 35,
6, 25,
6, 35,
10, 33,
10, 23,
10, 13,
8, 17,
8, 15,
8, 16,
[hr]
The finaly1values and finaly2values are:Output:finaly1values, finaly2values,
20, 30,
25, 35,
15, 30,
15, 17,
20, 50,
13, 33,
[hr]
Which results in: meanthickness = 14.5This is what I have before the For Loop in Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import pandas as pd import numpy as np filename = 'C:\\Users\\...\\scaletestmatrix.csv' data = pd.read_csv(filename, header = None ); ### Read the data file data = data.values ; ### Convert DataFrame to array of values (numbers) data = data.astype(np. float ); ### Convert from scientific notation to floating xvalues = data[:, 0 ]; ### Extract column 1 of the data as xvalues yvalues = data[:, 1 ]; ### Extract column 2 of the data as yvalues smallest = min (xvalues); ### Find the smallest xvalue, his may not be necessary in Python biggest = max (xvalues); ### Find the largest xvalue, this may not be necessary in Python startvalue = smallest ### This may not be necessary in Python. length = int ((biggest - smallest) + 1 ); ### This may not be necessary in Python |