How to find a value in a list?

I have a m-by-3 matrix of numbers (as below) and I would like to find a specific number in a row and then find the other numbers in that same row then write these in a seperate matrix. For example I want to look for the numbers connected to the number 4, in row 1 these will be 15 and 12 and in row 15 these will be 12 and 3. I then want a matrix called 4 to contain the values 15, 12 and 3. I do not want any repeats in this new matrix, hence 12 only appears once in the new matrix.
15 4 12
5 7 2
22 23 21
18 11 10
10 8 14
16 12 11
12 3 6
8 7 5
8 5 9
14 8 9
7 1 2
5 13 9
5 2 13
7 6 1
12 4 3

6 comentarios

Thomas
Thomas el 15 de Ag. de 2012
This looks like a Homework question. What have you done so far? Where are you getting stuck..
Thomas
Thomas el 15 de Ag. de 2012
@ Illam and Andrei: I thought HWk questions would not be answered on the forum without the poster showing what he/she has done...
Jordan
Jordan el 15 de Ag. de 2012
Homework question? I'm trying to write to write a code to describe how a bunch of points are connected. So far I have done a delaunay triangulation of the points which gives me the m-by-3 matrix and now I want to link the points, so point 4 is linked to point 15, 12 and 3. This is where I am stuck, I am unsure how you look up for each point which other points are connected. I think I would need some sort of for loop which which finds all the '4' values and then finds all the other values in those rows. Then repeat this for all the remaining values, 5,6,7...
Thanks.
Thomas
Thomas el 15 de Ag. de 2012
Jordan, sorry If my comment offended you, but this is the kind of problem I usually give the class I teach for introduction to Matlab and we do not want the forum to be a Hwk solving forum..
Matt Fig
Matt Fig el 15 de Ag. de 2012
Questions:
Do you want all of the numbers in a row that has the given number in it, or just those connected to it (adjacent neighbors)? Take the number 15 in your given list. Do you want to include both the 4 and the 12 from row 1 or just the 4?
What if we had a row like this:
4 4 6
Would we count 4 as a neighbor to 4 (Andrei's code will not)?
Will the real array you need to work with have numbers only on [1,23] or what range?
Thanks.
Jordan
Jordan el 16 de Ag. de 2012
@Matt I want to get all the numbers in the same row as a given number, so in the example you gave, I would want 4 and 12. Also you will never have a row where a number would be repeated as the numbers represent points in space and the 3 columns give three points which make a triangle. I want the code to be compatible with any range of numbers as the range will change depending on the input, so [1:i]. Thanks.

Iniciar sesión para comentar.

Respuestas (4)

Ilham Hardy
Ilham Hardy el 15 de Ag. de 2012

0 votos

Unchecked..
test_val = [15 4 12;5 7 2;22 23 21;18 11 10;10 8 14;16 12 11;12 3 6;8 7 5;8 5 9;14 8 9;7 1 2;5 13 9;5 2 13;7 6 1;12 4 3];
base_num = 4;
[r4 c4] = find(test_val==base_num);
matrx4 = unique(test_val(r4,:));
matrx4(matrx4==base_num)=[];
HTH, IH
Andrei Bobrov
Andrei Bobrov el 15 de Ag. de 2012
Editada: Andrei Bobrov el 16 de Ag. de 2012

0 votos

z = [...
15 4 12
5 7 2
22 23 21
18 11 10
10 8 14
16 12 11
12 3 6
8 7 5
8 5 9
14 8 9
7 1 2
5 13 9
5 2 13
7 6 1
12 4 3];
out = setdiff(unique(z(any(z == 4,2),:)),4);
ADD ( EDIT )
c = unique(z);
n = numel(c);
out = cell(1,n);
for j1 = 1:n
out{j1} = unique(z(conv2(+(z==c(j1)),[1 0 1],'same')>0));
end
Jordan
Jordan el 15 de Ag. de 2012

0 votos

Both of these solutions work for a single number, i.e 4, but is there a way which I can automate it so it creates a seperate new matrix for each value, 1-23. Could I possibly use a for loop?
Cheers

4 comentarios

Andrei Bobrov
Andrei Bobrov el 15 de Ag. de 2012
See ADD in my answer.
Matt Fig
Matt Fig el 15 de Ag. de 2012
Also, please look at my questions in the comment section. I want to be sure you get the correct approach...
Jordan
Jordan el 16 de Ag. de 2012
@Andrei, when I run this I get the error 'Cell contents assignment to a non-cell array object.'
Thanks
Andrei Bobrov
Andrei Bobrov el 16 de Ag. de 2012
Hi Jordan! Please use:
c = unique(z);
n = numel(c);
out = cell(1,n);
for j1 = 1:n
out{j1} = unique(z(conv2(+(z==c(j1)),[1 0 1],'same')>0));
end

Iniciar sesión para comentar.

Matt Fig
Matt Fig el 16 de Ag. de 2012

0 votos

For large data sets, this is the fastest I could come up with:
A = ceil(rand(19e4,3)*255); % Large data set on [1 255]
% Begin Code, store results in cell array T.
m = max(A(:));
T = cell(1,m);
for ii = 1:m
T{ii} = A(any(A==ii,2),:);
T{ii} = sort(T{ii}(:).');
T{ii} = T{ii}([~isempty(T{ii}) diff(T{ii})~=0]);
T{ii} = T{ii}(T{ii}~=ii);
end

Categorías

Más información sobre Interpolation en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 15 de Ag. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by