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

조회 수: 5 (최근 30일)
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

채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 5월 30일
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
Mary
Mary 2013년 5월 30일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Muruganandham Subramanian
Muruganandham Subramanian 2013년 5월 30일
편집: Muruganandham Subramanian 2013년 5월 30일
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

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by