how to add labels to the elements of a vector.
조회 수: 19 (최근 30일)
표시 이전 댓글
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.
댓글 수: 0
채택된 답변
Sindar
2020년 11월 30일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!