0
votes

i make a simulation to a gibbs sampling algorithm and the following code is part of the simulation :

mamodel=@(x)find(ismember(x(:,2:end),[1 0 0 0 0],'rows'))

if ~isempty(a)
    macount=a(mamodel(a),:)
else
    macount=zeros(1,size(a,2))
end

h(i,:)=(macount)

the first line is to find a certain row from simulation results the problem is when this row is not in the results the smulation stops because the result is Empty matrix: 0-by-6 , i tried isempty function and also the simulation stops!! i want to make the simulation ignore the results and continue to next step

1
Please format your code as code. Above the edit box there are a number of icons, the one that looks like {} is used to mark text as code.High Performance Mark
the question doesn't seem like it has anything to do with Gibbs sampling. What are your conditional distributions? (Or change the title of the question.)Memming

1 Answers

0
votes

From what I understand, your problem rises in the line h(i, :) = (macount) because there is an inconsistency with the dimensions.

Take into account that, with isempty what you are checking is if the variable a is empty, but then you are searching in it a given row, and then you can find yourself in the situation where the variable a is not empty, but it does not contain the row you are searching for and then you get an Emtpy matrix: 0-by-1.

So if I am right understanding your problem, you should be fine if you are careful with what things you check and at what point you check them.

If you first try to find the result in the matrix a, and then you check the emptiness of the result, you can avoid the empty variable problem. In the following piece of code, that is done (changing ~isempty by a test on the number of elements of the variable, but that is just a minor change, in this case at least):

macount = a(mamodel(a), :);
if numel(macount) == 0
    macount = zeros(1, size(a, 2));
end
h(i, :) = macount;

I don't know if this will fit your simulations, but it should fix the problems with the dimensions.