Add indicative labels to vector values

조회 수: 1 (최근 30일)
LH
LH 2022년 10월 12일
답변: Stephen23 2022년 10월 12일
Hi,
If I have two vectors:
A1 = [ 1 ; 2 ; 3 ];
A2 = [ 4 ; 5 ; 6 ];
Concatenating these two vectors gives:
A = [A1 ; A2];
I want to add another column to this vetoc that represents indicative binary labels, mainly either 0 or 1, so that I know from which vector this element corresponds to, i.e., I need to produce something like the following
A_labeled = [1 1;
2 1;
3 1;
4 0;
5 0;
6 0];
Any help would be appreicted.

답변 (3개)

David Hill
David Hill 2022년 10월 12일
A1 = [ 1 ; 2 ; 3 ];
A2 = [ 4 ; 5 ; 6 ];
A_labeled=[A1,ones(size(A1));A2,zeros(size(A2))]
A_labeled = 6×2
1 1 2 1 3 1 4 0 5 0 6 0

Chunru
Chunru 2022년 10월 12일
A1 = [ 1 ; 2 ; 3 ];
A2 = [ 4 ; 5 ; 6 ];
A_labeled = [A1 ones(size(A1)); A2 zeros(size(A2))]
A_labeled = 6×2
1 1 2 1 3 1 4 0 5 0 6 0

Stephen23
Stephen23 2022년 10월 12일
A1 = [1;2;3];
A2 = [4;5;6];
A1(:,2) = 0;
A2(:,2) = 1;
A = [A1;A2]
A = 6×2
1 0 2 0 3 0 4 1 5 1 6 1

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by