Find the closest match values in cell array

Hi guys, I got a cell array and I want to find the index of the array that closest match my defined values. Here is my cell array:
A =
[565.11,579.965]
[567.016,581.921]
[574.162,589.256]
575.909
[568.804,583.373]
[570.197,584.803]
[566.005,576.676,587.758]
As you can see the length of each row is not the same, that's why I couldn't put it in the form of a normal array. So, if I want to find the index that is the closest match 567.5 and 586.5. How can I do that?
Thanks!

Respuestas (2)

the cyclist
the cyclist el 10 de Mzo. de 2017
Here's one way:
A = {
[565.11,579.965]
[567.016,581.921]
[574.162,589.256]
575.909
[568.804,583.373]
[570.197,584.803]
[566.005,576.676,587.758]};
val = 567.5;
[~,indexToClosestMatch] = min(cellfun(@(x)min(abs(x-val)),A));

3 comentarios

Catherine
Catherine el 10 de Mzo. de 2017
Hi the cyclist, thank you for your reply. I need to find the index that would match both values (567.5 AND 586.5), not just one of them. Would that be possible? Thanks!
So what's wrong with just doing it twice???
val = 567.5;
[~,indexToClosestMatch1] = min(cellfun(@(x)min(abs(x-val)),A));
val = 586.5;
[~,indexToClosestMatch2] = min(cellfun(@(x)min(abs(x-val)),A));
Star Strider
Star Strider el 10 de Mzo. de 2017
She’s looking for a row match with an impossible condition.

Iniciar sesión para comentar.

Star Strider
Star Strider el 10 de Mzo. de 2017
‘How can I do that?’
One approach:
A = {[565.11,579.965]
[567.016,581.921]
[574.162,589.256]
575.909
[568.804,583.373]
[570.197,584.803]
[566.005,576.676,587.758]};
v = [567.5 586.5];
sz = cellfun(@(x)size(x,2), A, 'Uni',0); % Calculate Sizes Of Vectors
A_idx = cellfun(@(x)eq(x,2), sz, 'Uni',0); % Select Only Elements Of ‘A’ With 2 Columns
A_sel = A(cell2mat(A_idx)); % Elements Of ‘A’ Meeting Criteria
D = pdist2(v,cell2mat(A_sel)); % Distance Between ‘v’ And Elements Of ‘A’
[~,MinIdx] = min(D); % Indes OF Minimum Distance
A_closest = A_sel{MinIdx} % Element Values Of Minimum Distance
A_closest =
570.2 584.8

5 comentarios

Catherine
Catherine el 10 de Mzo. de 2017
Hi Star Strider, thanks for the answer. It is very close to what I am after! If I understand the code correctly, seems like you have selected only the first 2 columns of the cell array. But I do need it to be able to scan through the columns. In this case, the answer should be the last row of A with 576.676 and 587.758 in the 2nd and 3rd column; these should be the closest to 567.5 and 586.5.
Star Strider
Star Strider el 10 de Mzo. de 2017
‘last row of A with 576.676 and 587.758 in the 2nd and 3rd column’
It would have been helpful if you had mentioned that in your original Question. Such details are important.
Cell array indexing is difficult, and that is not possible.
Catherine
Catherine el 15 de Mzo. de 2017
Sorry for the lack of the details. Thanks for your help anyways :)
Star Strider
Star Strider el 15 de Mzo. de 2017
No worries. My pleasure.
Catherine
Catherine el 15 de Mzo. de 2017
Hi Star Strider, I am just wondering. Would it be possible, if I find the closest match in the first two columns first and store that information in an array, lets say B. Then go on and find the closest match for the 2nd and 3rd column and store that in array B and find the smallest number of the two to get the information? I don't know if that make sense.

Iniciar sesión para comentar.

Categorías

Preguntada:

el 10 de Mzo. de 2017

Comentada:

el 15 de Mzo. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by