How to match an input value with values stored in array or a file

9 views (last 30 days)
Hi everyone I am new to Matlab. I have a matrix consists of four columns. The first column is the object number, the second column is the x-coordinate of the object, the third column is the y-coordinate of the object and the fourth column is the size of the object.
I am measuring the x and y coordinates and the size of objects in the room. For each measured object, I want to compare the x and y coordinate of this object with the x and y coordinates of all the objects in the array. If these match with the x and y of any object in the array then I will put the highest size in the fourth column.
If the x and y coordinates of the object didn’t match any object in the array, then I want to add this object in a new row in the same array with its x and y and size.
For example A=[1, 3, 4, 1.5; 2, 4, 5, 1.6; 3, 5, 7, 2.2]
If I measure an object with x=3, y=4, and size=2.5 then the x and y for this object will match with the object in the first row and A will be
A=[1, 3, 4, 2.5; 2, 4, 5, 1.6; 3, 5, 7, 2.2]
if the new object has x=6, y=7, size=1.5, this object will not match with any one in the array and it will be added in a new row and A will be
A=[1, 3, 4, 2.5; 2, 4, 5, 1.6; 3, 5, 7, 2.2; 4, 6, 7, 1.5]
could any one please help me to write the codes for my problem. your help is highly appriciated

Accepted Answer

Andrei Bobrov
Andrei Bobrov on 30 May 2013
A=[1, 3, 4, 1.5; 2, 4, 5, 1.6; 3, 5, 7, 2.2]
newobjects = [ 3, 4, 4.5; 7, 7, 2.8]
B = [A(:,2:end);newobjects];
[a1,b2] = unique(B(:,1:end-1),'last','rows');
out = [(1:size(a1,1))', a1,B(b2,end)];
  1 Comment
Mary
Mary on 30 May 2013
thank you Andrei for your help.
I still have to put the biggest size of the object in the fourth column when there is match with x and y of the object in the array

Sign in to comment.

More Answers (1)

Muruganandham Subramanian
Muruganandham Subramanian on 30 May 2013
Edited: Muruganandham Subramanian on 30 May 2013
A=[1, 3, 4, 1.5;
2, 4, 5, 1.6;
3, 5, 7, 2.2]
A_size=size(A);
inp_1=input(' ');
inp_2=input(' ');
inp_3=input(' ');
for i=1:A_size(:,1)
for j=1:A_size(:,1)
if A(i,j)==(inp_1,inp_2) && j~=1
A(i,j+1)=inp_3;
else
A(i+1,j+1)=[j+1, inp_1, inp_2, inp_3];
end
I dont have matlab to check this, try this

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!