Rename the indices of a vector in a certain way. (2024)

27visualizaciones (últimos 30días)

Mostrar comentarios más antiguos

LH hace alrededor de 3 horas

  • Enlazar

    Enlace directo a esta pregunta

    https://es.mathworks.com/matlabcentral/answers/2134501-rename-the-indices-of-a-vector-in-a-certain-way

  • Enlazar

    Enlace directo a esta pregunta

    https://es.mathworks.com/matlabcentral/answers/2134501-rename-the-indices-of-a-vector-in-a-certain-way

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 Rename the indices of a vector in a certain way. (2). 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

%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

Iniciar sesión para comentar.

Iniciar sesión para responder a esta pregunta.

Respuesta aceptada

Garmit Pant hace 26 minutos

  • Enlazar

    Enlace directo a esta respuesta

    https://es.mathworks.com/matlabcentral/answers/2134501-rename-the-indices-of-a-vector-in-a-certain-way#answer_1481266

  • Enlazar

    Enlace directo a esta respuesta

    https://es.mathworks.com/matlabcentral/answers/2134501-rename-the-indices-of-a-vector-in-a-certain-way#answer_1481266

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

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

  • Enlazar

    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 Rename the indices of a vector in a certain way. (5). If we extract the unique values of this vector we will get Rename the indices of a vector in a certain way. (6) 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: Rename the indices of a vector in a certain way. (7)?

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

  • Enlazar

    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.


Translated by Rename the indices of a vector in a certain way. (9)

Rename the indices of a vector in a certain way. (10)

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

Rename the indices of a vector in a certain way. (2024)

References

Top Articles
Kingdom Hearts 20th Anniversary | SQUARE ENIX
„Kingdom Hearts“-Reihenfolge: Alle Teile der Spielesaga im Überblick
Steve Bannon Issues Warning To Donald Trump
Jps Occupational Health Clinic
'That's Hilarious': Ahsoka's Ezra Bridger Actor Reveals Surprising True-To-Life Detail Behind Sabine Reunion Scene
Jeff Bezos Lpsg
Gasbuddy Costco Hawthorne
Congdon Heart And Vascular Center
Understanding Pickleball Court Dimensions: Essential Guide
Dr Paul Memorial Medical Center
The Land Book 9 Release Date 2023
Craigslist.com Seattle Wa
Espn Major League Baseball Standings
Apple Store Near Me Make Appointment
Practice Assist.conduit.optum
Kamala Harris is making climate action patriotic. It just might work
5Ive Brother Cause Of Death
Lucifer Season 1 Download In Telegram In Tamil
Hotleak.vip
Her Triplet Alphas Chapter 22
Publix Store 1304
Smile 2022 Showtimes Near Savoy 16
Rubmaps Springfield
Truist Drive Through Hours
Proctor Motors In Lampasas
craigslist: northern MI jobs, apartments, for sale, services, community, and events
Starlight River Multiplayer
Toernooien, drives en clubcompetities
Lox Club Gift Code
north bay garage & moving sales "moving" - craigslist
Leonards Truck Caps
Ixl.prentiss
Courtney Lynn Playboy
Meritas Health Patient Portal
Shruti Rajagopalan — On Spotting Talent, And Making Sense of Rising India (#152)
Educational Outfitters Denver
Ixl Ld Northeast
Natalya's Vengeance Set Dungeon
Paper Io 2 Unblocked Games Premium
Southeast Ia Craigslist
Osceola County Addresses Growth with Updated Mobility Fees
Ces 2023 Badge Pickup
Texas State Academic Probation
Blow Dry Bar Boynton Beach
Sparkle Nails Phillipsburg
Drew Gulliver Bj
Dungeon Family Strain Leafly
Criagslist Orlando
Ups Carrier Locations Near Me
What Time Does Walmart Auto Center Open
Redbox Walmart Near Me
The Ultimate Guide to Newquay Surf - Surf Atlas
Latest Posts
Article information

Author: Amb. Frankie Simonis

Last Updated:

Views: 6577

Rating: 4.6 / 5 (76 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Amb. Frankie Simonis

Birthday: 1998-02-19

Address: 64841 Delmar Isle, North Wiley, OR 74073

Phone: +17844167847676

Job: Forward IT Agent

Hobby: LARPing, Kitesurfing, Sewing, Digital arts, Sand art, Gardening, Dance

Introduction: My name is Amb. Frankie Simonis, I am a hilarious, enchanting, energetic, cooperative, innocent, cute, joyous person who loves writing and wants to share my knowledge and understanding with you.