In an assignment, number of elements in A and B must be same

1 次查看(过去 30 天)
Hello
I am reproducing the Bayesian Change Detection code by Adams and MacKay. The following is a code for a time series data tem of size T.
T = 3600;
lambda = 200;
window_size=100;
hazard_func = @(r) constant_hazard(r, lambda); % a separate function
R = zeros([T+1]);
R(1,1) = 1;
muT = mean(tem(window_size:end));
stdT=std(tem(window_size:end));
alphaT = 1;
mins = zeros([T+1]);
for t=1:T
predprobs = studentpdf(tem(t), muT, stdT, 2 * alphaT); % studentpdf is a separate function
H = hazard_func([1:t]');
R(2:t+1,t+1) = R(1:t,t) .* predprobs .* (1-H);
R(1,t+1) = sum( R(1:t,t) .* predprobs .* H );
R(:,t+1) = R(:,t+1) ./ sum(R(:,t+1));
muT=[muT; mean(tem(1:t))]
stdT=[stdT; std(tem(1:t))];
alphaT = [alphaT; alphaT + 0.5];
mins(t) = find(R(:,t)==min(R(:,t)));
end
I keep getting the error
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in bayesian_change (line 84)
mins(t) = find(R(:,t)==min(R(:,t)));
I've checked multiple times and the dimensions of both mins and R are the same. I know it is tough to understand the code without prior knowledge, but can anyone help me figure out why I keep getting this error even though the dimensions are the same?
Thanks!

采纳的回答

Star Strider
Star Strider 2016-7-19
This line:
mins(t) = find(R(:,t)==min(R(:,t)));
will return all indices equal to ‘min(R(:,t))’. Apparently, there are more than one. If you want to return all of them, the easiest way is to do that is to use a cell array for ‘mins’:
mins{t} = find(R(:,t)==min(R(:,t)));
If you only want the index of the first minimum in the column, you can also just use the min function:
[~,mins(t)] = min(R(:,t));
NOTE This is UNTESTED CODE but should work.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by