I have a problem using splitapply for function mean.
Error using splitapply (line 111)
For N groups, every integer between 1 and N must occur at least once in the vector of group numbers.
Error in try_31032019 (line 65)
LPInew = splitapply(@mean,LPI,bins_index);
G1 = findgroups(amax2);
bins_index = discretize(G1,amaxgrid);
LPInew = splitapply(@mean,LPI,bins_index);
CSR_new = splitapply(@mean,CSR,bins_index);
constN1_60_new = splitapply(@mean,N1_60,bins_index);
constFC_new = splitapply(@mean,Const,bins_index);
Is there any one can help me to solve my problem?

7 Kommentare

bins_index = findgroups(discretize(G1,amaxgrid));
Skydriver
Skydriver am 25 Jun. 2019
It has been not working right know. Is there any solution?
I have modified with this function:
Mwgrid = linspace(4.75,8.25,8);
amaxgrid = linspace(0.19,0.42,36);
G1 = findgroups(amax);
bins_index = discretize(G1,amaxgrid);
LPInew = splitapply(@mean,LPI,bins_index);
HereQL_new_1 =zeros(size(grid));
for i=1:length(LPInew)
for j=1:length(Mwgrid)
QL_new_1(i,j) = Coeff(1) + Coeff(2)*constN1_60 + Coeff(3).*constFC + ...
Coeff(4).*log(LPInew(i)) + Coeff(5).*log(Mwgrid(j)) + Coeff(6).*log(constDi);
end
end
QL_zeroes=QL_new_1; % import
QL_zeroes(QL_zeroes<0)=0; % import
PL = normcdf(QL_zeroes);
But the bins_index become NAN for all layer.
I need to get LPInew is 36 x 1
Herewith I attach list of amax and LPI
I wish walter robertson or other can help me
Walter Roberson
Walter Roberson am 25 Jun. 2019
You should not use @mean for this purpose: you should use @(x) mean(x,1) to force mean along the first dimension. Otherwise if you happen to get a group that has only a single row, then that would show up as mean(a_row_vector) which would be interpreted as mean of the vector, giving a scalar result, whereas mean() of a 2D would be mean per column giving a vector result.
Skydriver
Skydriver am 25 Jun. 2019
And the syntax like this:
G1 = findgroups(amax);
bins_index = discretize(G1,amaxgrid);
LPInew = splitapply(@(36,mean(36,1);LPI;bins_index));?
What you intended to do in your code is not clear for me. For example, I'm not sure how to fix the following 3 lines:
amaxgrid = linspace(0.19,0.42,36);
G1 = findgroups(amax);
bins_index = discretize(G1,amaxgrid);
Looking at your data file G1 should be integer 1~7, since amax consists of 7 different values. On the otherhand, amaxgrid becomes 1-by-36 double array between 0.19~0.42.
So I have no idea what you intended to do in the 3rd line. Could you tell us more details on what kind of calculation shall be done on your data?
Skydriver
Skydriver am 26 Jun. 2019
Bearbeitet: Skydriver am 26 Jun. 2019
I want to develop 3d figure with x absis is amaxgrid, y ordinat is Mw grid and to match with the z like state bellow. In further I have to decided that QL should be representing in the z direction with matrix of grid as 36 * 7. Yess I have small mistake to decide 7 not 8.
with this coding:
Mwgrid = linspace(4.75,8.25,7);
amaxgrid = linspace(0.19,0.42,36);
grid = meshgrid(Mwgrid,amaxgrid);
QL_new_1 =zeros(size(grid));
for i=1:length(LPInew)
for j=1:length(Mwgrid)
QL_new_1(i,j) = Coeff(1) + Coeff(2)*constN1_60 + Coeff(3).*constFC + ...
Coeff(4).*log(LPInew(i)) + Coeff(5).*log(Mwgrid(j)) + Coeff(6).*log(constDi);
end
end
QL_zeroes=QL_new_1; % import
QL_zeroes(QL_zeroes<0)=0; % import
PL = normcdf(QL_zeroes); % import
[Xq_cond1,Yq_cond1] = meshgrid(linspace(min(Mwgrid),max(Mwgrid),8),linspace(min(amaxgrid),max(amaxgrid),36)); %import
Vq_cond1 = interp2(Mwgrid, amaxgrid, PL,Xq_cond1,Yq_cond1,'cubic');
Vq_cond2 = interp2(Mwgrid, amaxgrid, PL_new,Xq_cond1,Yq_cond1,'cubic')
figure(4)
surf(Xq_cond1, Yq_cond1, Vq_cond1)
hold on
xlim([4.75 7.6])
xlabel('Magnitude (Mw)');
ylim([0 3])
ylabel('Ground Surface Acceleration (a_{max})');
zlim([0 1])
zlabel('Conditional probability of a_{max} and Mw');

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Mil Shastri
Mil Shastri am 12 Okt. 2019

1 Stimme

The group numbers provided to splitapply need to be every integer between 1 to N. For example:
This will work:
splitapply(@mean,1:5,[2,2,3,1,1])
and this will not because the group number 3 is missing:
splitapply(@mean,1:5,[2,2,4,1,1])

2 Kommentare

Christoper Angelo Malayan
Christoper Angelo Malayan am 10 Jan. 2021
Is there a way for splitapply to SKIP this missing number and proceed to the next?
Walter Roberson
Walter Roberson am 10 Jan. 2021
No.
You can use findgroups on your current non-consecutive numbers.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by