MATLAB Answers

AA
0

Finding the row number with two conditions in a matrix

Asked by AA
on 25 Oct 2017
Latest activity Commented on by Star Strider
on 25 Oct 2017
outData=[733458 91 1.01510000000000 1.01680000000000 1.01490000000000 1.01520000000000 1.01585000000000
733458 112 1.01620000000000 1.01620000000000 1.01400000000000 1.01610000000000 1.01510000000000
733458 135 1.01440000000000 1.01540000000000 1.01430000000000 1.01480000000000 1.01485000000000
733458 154 1.01420000000000 1.01580000000000 1.01410000000000 1.01420000000000 1.01495000000000
733459 168 1.01550000000000 1.01570000000000 1.01490000000000 1.01560000000000 1.01530000000000
733459 173 1.01550000000000 1.01570000000000 1.01490000000000 1.01560000000000 1.01530000000000
733459 174 1.01550000000000 1.01570000000000 1.01490000000000 1.01560000000000 1.01530000000000
733459 188 1.01550000000000 1.01570000000000 1.01490000000000 1.01560000000000 1.01530000000000
733460 157 1.01550000000000 1.01570000000000 1.01490000000000 1.01560000000000 1.01530000000000]
x=157;
result=[733458;733459];
for i=1:2
idx=find((outData(:,1) == result(i,1)) & ((x-outData(:,2)).^2) == min( (x-outData(:,2)).^2));
idx1{i}=idx;
end
There are two problems with the code above. First of all the idx code does not give me always the accurate row number and sometimes misses out a few rows. Secondly, I want to display all the rows that have the date serial number in results and that are closest to x in column 2. So in this case that should be 4 and 5 but I only get 4. Can anyone help?

  0 Comments

Sign in to comment.

2 Answers

Answer by Star Strider
on 25 Oct 2017
 Accepted Answer

I am not certain what you want to do.
Calculating the separate parts of the conditions that create ‘idx’ separately and looking at them may give you some idea of where the problem is:
x=157;
result = [733458;733459];
idx1 = bsxfun(@eq, outData(:,1), result(1));
idx2 = bsxfun(@eq, outData(:,1), result(2));
idxx = bsxfun(@eq, (x-outData(:,2)).^2, min((x-outData(:,2)).^2));
You can then combine the individual logical vectors to give you the result you want as ‘idx’. This is probably a bit easier to debug than your original code.

  2 Comments

thanks for the answer but this is not I was looking for. I just need the closest x values for 733458 and 733459. with your method I also include the other x values from the other date serials which is not what I want.
I simply copied the code you posted and created the logical vectors. The idea is that you can see what they do so you could troubleshoot your criteria.
‘I want to display all the rows that have the date serial number in results and that are closest to x in column 2.’
In your code, you are setting them to be exactly equal to the minimum, in this instance, 157. If you want to set them to a range, for example within 20 of the minimum, change the ‘idxx’ assignment to:
idxx = bsxfun(@lt, (x-outData(:,2)).^2, min((x-outData(:,2)).^2) + 20);
I still do not have a sense of what you want to do.

Sign in to comment.


Answer by Andrei Bobrov
on 25 Oct 2017

x=157;
result=[733458;733459];
[~,g] = ismember(outData(:,1),result);
ii = find(x == outData(:,2));
g(ii) = g(ii - 1);

  0 Comments

Sign in to comment.