27visualizaciones (últimos 30días)
Mostrar comentarios más antiguos
LH hace alrededor de 3 horas
Comentada: Garmit Pant hace alrededor de 2 horas
Respuesta aceptada: Garmit Pant
Abrir en MATLAB Online
Hi all,
My code below has a vector A and its index vector idx. It goes throguh the elements of A and when it finds a zero element, it removes its index from the orginal index vector.
My problem here is: suppose the updated index vector is . How can I rename this vector so it increases in an acending order? For example: the first element in the updated index vector is 1, I want this to be 1, the second element in the updated index vector is 3, I want this to become 2, and so on so forth.
%define the index vector
idx = [1 2 3 4 5];
%define the vector
A = [5 0 6 0 3];
%initialise the vector that has all indices of zero elements
zero_indices = [];
%go through the elements of vector A
for uu = 1:numel(A)
%if the element is zero
if A(uu)==0
%assign its index to the zero-elements index vector
zeroindex = uu;
%collect all results from iterations
zero_indices = [zero_indices ; zeroindex];
end
end
%update the original index vector by removing the indicies that correspond
%to zero elements
idx(zero_indices) = [];
%rename the indices os that they beocome 1, 2, 3, ...
Any help would be appreicted.
Thanks.
0 comentarios Mostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos
Mostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos
Iniciar sesión para comentar.
Iniciar sesión para responder a esta pregunta.
Respuesta aceptada
Garmit Pant hace 26 minutos
Abrir en MATLAB Online
Hello LH
Given that you have extracted the indices of non-zero elements in the vector ‘A’, you can rename the index vector to have ascending values using the following code-snippet:
% Given non-zero value indices are [1 3 5]
idx = [1 3 5];
% Rename the indices so that they become 1, 2, 3, ...
new_idx = 1:numel(idx);
% Display the updated index vector
disp('Updated index vector:');
Updated index vector:
disp(new_idx);
1 2 3
I hope you find the above explanation and suggestions useful!
2 comentarios Mostrar NingunoOcultar Ninguno
Mostrar NingunoOcultar Ninguno
LH hace 13 minutos
Enlace directo a este comentario
https://es.mathworks.com/matlabcentral/answers/2134501-rename-the-indices-of-a-vector-in-a-certain-way#comment_3202731
Abrir en MATLAB Online
Thanks Garmit, it's indeed helpful.
What if the problem becomes a bit more complicated and the index vector is like . If we extract the unique values of this vector we will get using:
idx_unique = unique(idx);
which will give us the three main values. Renameing these indices using your line:
% Rename the indices so that they become 1, 2, 3, ...
new_idx = 1:numel(idx_unique);
But now, how can we apply this on the orginal idx vector so it besomes: ?
Thanks.
Garmit Pant hace 3 minutos
Enlace directo a este comentario
https://es.mathworks.com/matlabcentral/answers/2134501-rename-the-indices-of-a-vector-in-a-certain-way#comment_3202736
Abrir en MATLAB Online
This can be solved by mapping the values of the 'idx' vector to ascending values. The following code snippet does the same:
% Using the idx vector given in the above comment
idx = [1 3 3 5 1 5 3 1]
idx = 1x8
1 3 3 5 1 5 3 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Extract unique values from the updated index vector
idx_unique = unique(idx);
% Rename the indices so that they become 1, 2, 3, ...
new_idx = 1:numel(idx_unique);
% Create a mapping from unique values to new indices
mapping = containers.Map(idx_unique, new_idx);
% Apply the mapping to the original index vector to get the renamed indices
renamed_idx = arrayfun(@(x) mapping(x), idx);
% Display the updated index vector
disp('Renamed index vector:');
Renamed index vector:
disp(renamed_idx);
1 2 2 3 1 3 2 1
Iniciar sesión para comentar.
Más respuestas (0)
Iniciar sesión para responder a esta pregunta.
Ver también
Etiquetas
- index
- matlab coder
- vector
- while loop
- remove
- delete
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Se ha producido un error
No se puede completar la acción debido a los cambios realizados en la página. Vuelva a cargar la página para ver el estado actualizado.
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
América
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia-Pacífico
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Comuníquese con su oficina local