Replace each element of a matrix with the number it is closest to in a set of discrete values without using for loop
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ted
el 18 de Ag. de 2017
Editada: Andrei Bobrov
el 19 de Ag. de 2017
I have a set of discrete values in the row vector <list>. I want to take a vector M of real doubles, and for each element M(j) in M I want to replace M(j) with the element in <list> that is closest to M(i) on the number line. For example, if my list was [2 4] and my matrix was [1.1, 3.2, 5, -17], the resulting matrix would be [2, 4, 4, 2].
I know I can do this with a for loop:
for j=1:length(M)
[m mn]=min(abs(list-M(j)));
M(j)=list(mn);
end
Is there any way to do this without a for loop?
0 comentarios
Respuesta aceptada
Andrei Bobrov
el 18 de Ag. de 2017
Editada: Andrei Bobrov
el 19 de Ag. de 2017
A = [2 4], B = [1.1, 3.2, 5, -17]
[~, ii] = min(abs(bsxfun(@minus,B(:),A(:).')),[],2); % MATLAB <= R2016a
[~, ii] = min(abs(B(:) - A(:).'),[],2); % MATLAB >= R2016b
out = A(ii)
or
F = griddedInterpolant(A,'nearest');
out = F(B)
or
out = interp1(A,B,'nearest','extrap');
Más respuestas (1)
MG Poirot
el 18 de Ag. de 2017
Editada: MG Poirot
el 18 de Ag. de 2017
I think I've got a simple solution for you by finding the minimal difference after matrix subtraction:
list = [2 4]';
M = [1.1 3.2 5 -17];
A = repmat(M,numel(list),1)
B = repmat(list,1,numel(M))
dif = abs(A-B)
[~, I] = min(dif,[],1)
list(I)'
-mgp
0 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!