excel averageif function in matlab

3 次查看(过去 30 天)
for cata = 1:281
mydata = mean(cellfun(@(x) x(cata,2), thecells));
data2 = cell2mat(num2cell(mydata));
plot(cata,data2)
end
mydata is 5000*2 double look like
time data
1 225.42968
1 453.4097824
1 254.1819706
2 164.3800456
2 411.2133781
2 266.0093299
2 399.3169552
3 155.2650000
4 322.7854587
4 366.5487525
i try many methods but cannot create a loop same as averageif function in excel. my prof tell me to put nan for numbers to create same size for each time. But I am wondering another ways of the averageif function in excel.

采纳的回答

Star Strider
Star Strider 2018-6-29
I am not certain what structure your original data are.
Try this:
C = {1 225.42968
1 453.4097824
1 254.1819706
2 164.3800456
2 411.2133781
2 266.0093299
2 399.3169552
3 155.2650000
4 322.7854587
4 366.5487525};
M = cell2mat(C);
Out = accumarray(M(:,1), M(:,2), [], @mean);

更多回答(1 个)

Steven Lord
Steven Lord 2018-6-29
If you're using release R2018a and your data is stored as a table, use groupsummary.
time = [1; 1; 1; 2; 2; 2; 2; 3; 4; 4];
data = [225.42968;
453.4097824;
254.1819706;
164.3800456;
411.2133781;
266.0093299;
399.3169552;
155.2650000;
322.7854587;
366.5487525];
T = table(time, data)
groupsummary(T, 'time', 'mean')
Or you could use splitapply (since your times are already group numbers; if they weren't a vector of positive integer values you would need to use findgroups first.)
meantime = splitapply(@mean, data, time)

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by