Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Find

Asked by AP on 6 Jul 2011

I have a matrix 10000×2, named A. The first column is x and the second column is y. I am trying to find the number of element where the following condition holds:

y<x+b && y>x-b

where b is a constant. I used FIND command to achieve this. I want to make sure it is correct. Thank you very much.

numel(find((A(:,2)<A(:,1)+b) & (A(:,2)>A(:,1)-b)))

I would appreciate quick responses :))))))

0 Comments

AP

Tags

Products

No products are associated with this question.

2 Answers

Answer by Matt Fig on 6 Jul 2011

Use & instead of && when dealing with array comparisons.

Also, this is faster:

sum((A(:,2)<A(:,1)+b) && (A(:,2)>A(:,1)-b))

1 Comment

AP on 6 Jul 2011

that was soooo quick thanks.

Matt Fig
Answer by Paulo Silva on 6 Jul 2011
a=randi([1 10],10,2); %make up some data
x=a(:,1);
y=a(:,2);
b=5;
sum(y<x+b & y>x-b)

0 Comments

Paulo Silva

Contact us