how to add labels to the elements of a vector.

25 views (last 30 days)
hello, everything okay?
Is it possible to label the elements of a vector? To later know in what position is it at a certain moment?
for exemple:
A=[4, 6 ,8 ,14, 90 ] (A contains the vector information set let's say A1 and A2, when A1=[4, 6 ,90] and A2=[8, 14])
Assign the position indices of each element of the vector. LabelA1=[1, 2,5] LabelA2=[3 ,5] LabelA=[ 1, 2,3,4,5]
if I do determine tasks with the vector and it looks like this A=[0 6 8 0 9 ] in another word A(1)=0 and A(4)=0
so A=[ 6 8 9 ] . LabelA1(1)=0 and LabelA2(1)=0 implies that LabelA1=[2,5] LabelA2=[5]
I need to find from the labels that of A1 A2 the position it occupies after updating vector A.
thank you.

Accepted Answer

Sindar
Sindar on 30 Nov 2020
One option: store things in a table.
This particular setup may not be the most efficient, but hopefully it helps you start:
% build a table with columns for the original data, and whether it is part of A1 and/or A2
A_table = table([4; 6; 8; 14; 90],[true;true;false;false;true],[false;false;true;true;false],'VariableNames',["values","A1","A2"])
A_table =
values A1 A2
______ _____ _____
4 true false
6 true false
8 false true
14 false true
90 true false
% access "A" like this
A=A_table{:,1}
A = 5×1
4
6
8
14
90
% and A1 or A2 like this:
A1=A_table{A_table{:,"A1"},1}
A1 = 3×1
4
6
90
% label A1 can be calculated
LabelA1 = find(A_table{:,"A1"} & A_table{:,1}~=0)
LabelA1 = 3×1
1
2
5
% update A (silly example algorithm to replace)
A_table{:,1} = A_table{:,1}.^0.*[0 6 8 0 9 ]'
A_table =
values A1 A2
______ _____ _____
0 true false
6 true false
8 false true
0 false true
9 true false
% re-extract A, etc. as before (you could create functions for this)
A=A_table{:,1}
A = 3×1
6
8
9
A1=A_table{A_table{:,"A1"},1}
A1 = 2×1
6
9
LabelA1 = find(A_table{:,"A1"} & A_table{:,1}~=0)
LabelA1 = 2×1
2
5
An idea that might be useful, but I haven't coded/checked: add columns for LabelA1,LabelA2. These would contain either the original index or NaN. This would allow you to actually discard rows from the table
values A1 A2 LabelA1
______ _____ _____ _____
4 true false 1
6 true false 2
8 false true nan
14 false true nan
90 true false 5
  3 Comments
Sindar
Sindar on 2 Dec 2020
Some notes
  • you don't need to use "find" when indexing, the logical vector will work fine
  • Using A(i) as an index doesn't do what I think you want
A=[4, 6 ,8 ,14, 90 ] ;
A1=[4, 6 ,90] ;
A2=[8, 14]
Lbel=1:numel(A) % is a vector from 1 to the size of A containing the position of each element
% this sets the 4th (A(1)) element of Lbel to zero
Lbel(A(1))=0;
% this sets the 14th (A(4)) element of Lbel to zero,
% as well as any new elements needed to reach this point
Lbel(A(4))=0;
% this removes all zeroes, including the extra ones created by the last line
Lbel(Lbel==0)=[];
% set 1st element of A1, A2 to zero
A1(1)=0;
A2(1)=0;
% set the 0th element of A to zero... this should throw an error
A(A1(1))=0;
A(A2(1))=0;
A(A==0)=[];
A1(A1==0)=[];
A2(A2==0)=[];
[~,A1]=ismember(A1,Lbel);
[~,A2]=ismember(A2,Lbel);

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!