- you are having four for-loops, which can be put together. For-loops are slow and from your code I did not see why you made "a second round".
- you are writing for i = 1:length(T_A), which in the case of a vector of integers from one is the same as writing for i = T_A. Also you do not have to index into T_A/b_A to retrieve the data (T_A(k)) because it is always k or i
Unable to perform assignment because the size of the left side and right side are different
5 次查看(过去 30 天)
显示 更早的评论
I want to estimate a 3-parameter weibul distribution. And I got the warning "Consider Preallocating for speed",
so I create the lines with the zeros in the middle.
But for "params = zeros(n,length(b_A),length(T_A));" I get the warning "Unable to perform assignment because the size of the left side is 1-by-5 and the size of the right side is 1-by-3."
Can someone help me plz?
Also if you have any ideas for improving my code, I would appreciate that. I am new on matlab.
n= 10;
t0= 0.5;
b_A= 1:5;
T_A= 1:5;
LowerBound= [0 0 0];
rng('shuffle')
data= zeros(n,length(b_A),length(T_A));
params= zeros(n,length(b_A),length(T_A));
for k= 1:length(T_A)
for i= 1:length(b_A)
data(:,i,k) = wblrnd(b_A(i),T_A(k), [n,1]) + t0;
start= [b_A(i) T_A(k) t0];
custompdf = @(x,a,b,c) (x>c).*(b/a).*(((x-c)/a).^(b-1)).*exp(-((x-c)/a).^b);
opt = statset('MaxIter',1e5,'MaxFunEvals',1e5,'FunValCheck','off');
params(i,:,k) = mle(data(:,i,k),'pdf',custompdf,'start',start,'Options',opt,'LowerBound',LowerBound,'UpperBound',[Inf Inf min(data(:,i,k))])
end
end
for k= 1:length(T_A)
for i= 1:length(b_A)
params(i,4,k) = b_A(i);
params(i,5,k) = T_A(k);
params(i,6,k) = t0;
end
end
0 个评论
采纳的回答
Mara
2020-6-20
编辑:Mara
2020-6-20
Hello Mustafa, when you preallocate the variable params, you have a little twist in there, since you defined it as having the same size as data. However, params has b_A as first, n as second and T_A as third dimension. Later you retrieve data (mle...), it seems like you want to save it as the first 3 elements of the second dim of params. But on the right side you tell MATLAB you want to put it in all 10 columns(i,:,k), which gives you an error (also, you are only specifying the second dimension to element 6, if you don't want the rest to be zeros, you have to replace n by 6).
For the rest I changed some things I could quickly recognize as being redundant:
clear;
clc;
n = 10;
t0 = 0.5;
b_A = 1:5;
T_A = 1:5;
LowerBound= [0 0 0];
rng('shuffle');
data = zeros(n,length(b_A),length(T_A));
params = zeros(length(b_A),n,length(T_A));
for k= T_A
for i= b_A
data(:,i,k) = wblrnd(i,k, [n,1]) + t0;
start = [i k t0];
custompdf = @(x,a,b,c) (x>c).*(b/a).*(((x-c)/a).^(b-1)).*exp(-((x-c)/a).^b);
opt = statset('MaxIter',1e5,'MaxFunEvals',1e5,'FunValCheck','off');
params(i,1:3,k) = mle(data(:,i,k),'pdf',custompdf,'start',start,'Options',opt,'LowerBound',LowerBound,'UpperBound',[Inf Inf min(data(:,i,k))])
params(i,4,k) = i;
params(i,5,k) = k;
params(i,6,k) = t0;
end
end
I think it should still work as you want it, but you should check the arrays, better safe than sorry!
P.S I do not know about weibul distribution and whether your code does what it should, I just looked at the code itself.
sry for all the editing
5 个评论
Mara
2020-10-31
Hi Mustafa,
sorry for the late answer. At that point I am defining where to put the new data. We need to define how many and which rows and columns of the array result3p will be used. For the new data we need 5 rows and 6 columns and we need to put it behind the data that is already there from the previous loops.
So (k-1)*b_A calculates how many rows have already been filled up in the rounds before. For example if you run the k-loop for the third time, the first new line must be Nr. 11. Why? Because Loop k=1 used 5 rows and k=2 used another 5 rows. So I for k = 3 it is calculating (3-1)*5+1=11
the size of the second dimension of params3p is 6. So the code 1:size(params3p, 2) will be the vector 1, 2, 3, 4, 5, 6. These will be the columns for the data in result3p.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!