フィルターのクリア

how can I change a n*1 matrix to a n*6 matrix with a condition based on column value?

1 回表示 (過去 30 日間)
Al
Al 2022 年 7 月 28 日
回答済み: dleal 2022 年 7 月 28 日
Hi, i'm pretty new so any help would be appreciated.
I have 700000*1 matrix with the column values that are between 1 to 6 and i want to make a new matrix of 700000*6 (having 6 colulmns instead of 1) and the values of the columns in new matrix in each row would be 1,0 in a way that 1 would show the value in the original matrix.
for example :
original =
[ 1
2
1
3
5]
and i want the new matrix look like this :
[1 0 0 0 0 0
0 1 0 0 0 0
1 0 0 0 0 0
0 0 1 0 0 0
0 0 0 0 1 0]
I've tried by making a blank 700000*6 matrix first but i can't figure out how to put values in the new matrix the way i want.

採用された回答

dleal
dleal 2022 年 7 月 28 日
If you have the Statistics and Machine Learning Toolbox you can do use dummyvar:
A = [1,1,2,4,4, 5,6]';
>> dummyvar(A)
ans =
1 0 0 0 0 0
1 0 0 0 0 0
0 1 0 0 0 0
0 0 0 1 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1

その他の回答 (1 件)

David Hill
David Hill 2022 年 7 月 28 日
編集済み: David Hill 2022 年 7 月 28 日
n=randi(6,700000,1);
a=zeros(size(n,1),6);
a(:,n)=1;

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by