Rename the indices of a vector in a certain way.
Hi all,
My code below has a vector and its index vector . It goes throguh the elements of 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 , I want this to be , the second element in the updated index vector is , I want this to become , 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.Hi all,
My code below has a vector and its index vector . It goes throguh the elements of 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 , I want this to be , the second element in the updated index vector is , I want this to become , 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. Hi all,
My code below has a vector and its index vector . It goes throguh the elements of 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 , I want this to be , the second element in the updated index vector is , I want this to become , 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. index, matlab coder, vector, while loop, remove, delete MATLAB Answers — New Questions