: Indexing with NaN, and python zip function in matlab
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jakub Matousek
el 29 de Sept. de 2017
Respondida: Rik
el 2 de Nov. de 2017
Hi, I have matrices like this:
v = [4 10;20 8]
a = [1 2;2 2]
First problem is:
I want to do an operation simillar to this one:
b(:,1) = v(a(1,:),a(2,:))
Which yields :
b = [10 10;8 8]
But I would like it to function like a zip in python, so it should give me:
b = [10 8] or b = [10;8]
The second problem is some of the columns in the a matrix are NaNs so I cant index over them, I need to skip these in a way that I would get inf or NaNs in the matrix b... and I cant just delete these columns because I need the order and position of output stay the same. Is there any other way(better optimized for performance) than swapping NaNs for valid indexes and then go over output and change corresponding rows/columns back to NaNs?
0 comentarios
Respuesta aceptada
Rik
el 2 de Nov. de 2017
v = [4 10;20 8];
a = [1 2;2 2];
index=(sub2ind(size(v),a(1,:),a(2,:)));
result=nan(size(index));
result(~isnan(index))=v(index(~isnan(index)));
%result=[10 8];
v = [4 10;20 8];
a = [1 2;NaN 2];
index=(sub2ind(size(v),a(1,:),a(2,:)));
result=nan(size(index));
result(~isnan(index))=v(index(~isnan(index)));
%result=[NaN 8]
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Call Python from MATLAB 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!