Pergunta

I'm currently working on a cellular automata but i keep running into this problem. I have a matrix idxR which contains zero's and/or ones, depending on a probability process:

idxR = ((rRecr>rEmpty)&(rRecr>rAlgae)&(rRecr>rCoral));

Now i want to replace all ones in idxR with unique values and assign it to the variable colonies. I came up with the following:

colonies = idxR; 
no = sum(colonies(:)==1)
maxvalue = max(colonies(:));
replace = [1:no]+maxvalue;
ret = reshape(replace,no,1);
colonies(colonies==1) = colonies(colonies==1).*ret;

When i output colonies it gives me a matrix with just ones and zeros and not a matrix where all ones have been replaced with incremental values. I tried this code in a new file and assigned a matrix with random ones and zeros to idxR and then it seems to work. So i guess to problem lies with the matrix idxR in my automata. It might be worth mentioning that idxR is contained in a for loop.

Can somebody tell me how to fix this?

Foi útil?

Solução

You got the entire logic correct, except one minor flaw. You have idxR as a logical matrix. Hence colonies is a logical matrix too. Therefore, you get the expected output till the second-last line. Problem occurs on the last line, when you try to assign an array of numbers in which each number is greater than 1 (colonies(colonies==1).*ret;) to a logical matrix.

Elements greater than 1 are clipped to one and thus you see only zeros and ones. There is a simple workaround. Change the first line to

colonies = double(idxR);

P.S. The answer was right in front of you, you just didn't spot it. You had written:

I tried this code in a new file and assigned a matrix with random ones and zeros to idxR and then it seems to work.

The idxR matrix must have been of double datatype, if you used randi.

Outras dicas

Parag got it right. You have the solution there.

You can use the following code if you are looking for a more "organized" way to get to 'colonies' -

colonies = double(idxR);
maxvalue = max(colonies(:));
ind1 = find(idxR==1);
colonies(ind1)=maxvalue + (1:numel(ind1));
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top