lara
lara

Reputation: 63

Replace missing value in an array

I have an array A=[3 4 1 2 5 6] and another array B=[3 4 0 2 0 0]

I tried to find out the missing elements of B using setdiff function of Matlab.

missing_values=setdiff(A,B);

i got answer as

missing_values= 1 5 6

Now i want to replace zeros of array B with missing values.... can you help me?

i tried the code given below:

j=1;
missingvalues=setdiff(A,B)
idx= nwP4(2,: )== 0;
if(A(idx)==0)    
A(idx) = missingvalues(j);
j=j+1;    `enter code here`
end 

but it didn't work.

can you help me?

Upvotes: 0

Views: 88

Answers (2)

herohuyongtao
herohuyongtao

Reputation: 50657

You can do this:

B(B==0) = missing_values;

Or simply put them into one line:

B(B==0) = setdiff(A, B);

Edit: Just curious, maybe the example is over-simplified, it seems you eventually want B equal to A. If so, you can simply do:

B = A; 

Upvotes: 1

Anoop
Anoop

Reputation: 5720

Try this. I assume this is what you are trying to do.

A=[3 4 1 2 5 6]
B=[3 4 0 2 0 0]

missing_values=setdiff(A,B);
B(find(~B))=missing_values

Or to do it in one line,

B(find(~B))=setdiff(A,B);

This will leave B with

B = [3   4   1   2   5   6]

Upvotes: 1

Related Questions