Average product of arrays
Show older comments
Hi
I have an excel sheet having following informations;
flows occupancy
{6,18,17,14} {0.0383,0.13,0.1794,0.1522}
{12,14,12,5} {0.0717,0.0978,0.1117,0.0633}
{16,11,13,12} {0.1006,0.08,0.0978,0.1839}
In one column there is flows {x1,x2,x3,x4} and another column there is occupancy {y1,y2,y3,y4}
I need to make Matlab code as follows
averageproduct = average (x1*y1+x2*y2+x3*y3+x4*y4)
the number of rows are about more than 15000
Could you help me in this matter
2 Comments
Laxmi Bhatta
on 11 Mar 2023
Moved: Stephen23
on 11 Mar 2023
"I have an excel sheet ..."
What you uploaded is a CSV file. A CSV file is a text file that exists completely independently from MS Excel.
"In one column there is flows {x1,x2,x3,x4} and another column there is occupancy {y1,y2,y3,y4}"
Note that the CSV file columns FLOWS and OCCUPANCY do not have constant four terms each, e.g. from row 944 the number of terms increases from four to five, then from row 1896 it decreases back to four again. So your solution needs to take this into account (e.g. as my answer does).
For example, the term distribution for FLOWS is as follows:
T = readtable('pems_rawdata_NB.csv');
V = cellfun(@(t)numel(sscanf(strrep(t,'{',''),'%f,')),T.flows);
plot(V,'*r')
ax = gca;
ax.XAxis.Exponent = 0;
xlabel('Row Number')
ylabel('Number of Terms')
Accepted Answer
More Answers (1)
Shubham
on 10 Mar 2023
Here's a sample code in MATLAB that should achieve what you're looking for:
% Define the data as arrays
flows = {[6,18,17,14]; [12,14,12,5]; [16,11,13,12]};
occupancy = {[0.0383,0.13,0.1794,0.1522]; [0.0717,0.0978,0.1117,0.0633]; [0.1006,0.08,0.0978,0.1839]};
% Convert the cell arrays to matrices
flows_mat = cell2mat(flows);
occupancy_mat = cell2mat(occupancy);
% Calculate the average product using element-wise multiplication and sum
averageproduct = sum(flows_mat .* occupancy_mat, 2) / size(flows_mat, 2);
% Display the result
disp(averageproduct);
This code should work for any number of rows in your data. The code first converts the cell arrays to matrices using the cell2mat function, then calculates the average product using element-wise multiplication and the sum function. Finally, it divides the sum by the number of columns in the data to get the average.
Hope this helps!
3 Comments
Laxmi Bhatta
on 10 Mar 2023
Shubham
on 11 Mar 2023
Yes, you can modify the previous code to read data from a CSV file using the readtable function. Here is an updated version of the code:
% read data from CSV file
data = readtable('filename.csv');
% extract flows and occupancy from the table
flows = data{:, 'D'};
occupancy = data{:, 'E'};
% calculate the average product
averageproduct = mean(flows .* occupancy);
In this code, you should replace 'filename.csv' with the actual name of your CSV file. The readtable function creates a table from the CSV file, and then the {:,'D'} and {:,'E'} indexing expressions extract the data from the 'D' and 'E' columns, respectively. The rest of the code is the same as before - it calculates the average product using element-wise multiplication and the mean function.
Laxmi Bhatta
on 11 Mar 2023
Categories
Find more on Data Type Conversion 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!