tags:

views:

448

answers:

2
data = rand(N,2);             %# generate random points
index = (rand(N,1) <= p);     %# roll coins to pick with prob p
data(~index, :) = [];         %# keep only selected points
plot(x(index),y(index),'r*');

dist=sqrt((data(1,1)-data(~index,1))^2+(data(1,2)-data(~index,2))^2); % ques is what to put for data(~index,:) 's first or any element?

If we have to find distance between any point of data=rand(N,2) (say first) from any point of data(~index, :) = []; (say first of this also), how can we use data(~index, :) = []; matrix in following statement?

dist=sqrt((data(1,1)-data(~index,1))^2+(data(1,2)-data(~index,2))^2)

My question is as we are taking first element of data as data(1,1) and data (1,2), how do we take take first element of data(~index,:) matrix?

+2  A: 

I'm not quite sure what you want to do.

The command data(~index, :) = [] removes all rows for which the random number is larger than p, so after that command data will no longer be an N-by-2 matrix but it will be a smaller matrix. I'll assume you won't give that command, because afterwards you can no longer refer to the points in data that were removed.

If you want to take the distance between the first point in data and the first point in data selected by the random coin throw, one possibility is to use the find function (look it up to see what it does):

indexList = find(index);
firstIndex = indexList(1);
dist = sqrt((data(1,1)-data(firstIndex,1))^2 + (data(1,2)-data(firstIndex,2))^2);

The last line can also be written as:

dist = norm(data(1,:) - data(firstIndex,:));

If you want something else, please explain.

Jitse Niesen
A: 

Assuming that you by 'first element of data(~index,:)' you mean the first element for which ~index is true then the following would also work

firstValue = data(find(~index,1,'first'),:)
Azim

related questions