I have a vector (160,1) in size which include integer number from 1 to 4, I want to convert in logical array by replacing 1 to(1 0 0 0), 2 to (0 1 0 0), and so on; for classifiction propblem.

 採用された回答

Image Analyst
Image Analyst 2016 年 12 月 17 日

1 投票

Try this:
m = randi([1, 4], 160, 1)
% Get 3 bit array
characterArray = dec2bin(m)
% Tack on column of 0's to the left edge
characterArray = [repmat('0', 160, 1), characterArray]
logicalArray = logical(characterArray - '0')

5 件のコメント

Image Analyst
Image Analyst 2016 年 12 月 17 日
I created a "a vector (160,1) in size which include integer number from 1 to 4" just like you said. Apparently you're starting with something else. Please give your code where you create that column vector.
Najiya Omar
Najiya Omar 2016 年 12 月 17 日
it works perfectly now, i just create differnt size vector to test it, and I forgot to change it in the code.
thanks
Image Analyst
Image Analyst 2016 年 12 月 17 日
To be more robust for different sized vectors, use size() or length() when you call repmat():
m = randi([1, 4], 160, 1)
% Get 3 bit array
characterArray = dec2bin(m)
% Tack on column of 0's to the left edge
characterArray = [repmat('0', length(m), 1), characterArray]
logicalArray = logical(characterArray - '0')
Najiya Omar
Najiya Omar 2016 年 12 月 17 日
thank you, it's really help
Carlos Orduno
Carlos Orduno 2021 年 9 月 3 日
Thanks for sharing,
This solution gave an error, so I modified it slighly, just in case someone is having any problem.
% Generate random values
m = randi([1, 4], 160, 1)
% Get 4 bit array
characterArray = dec2bin(m,4)
% convert to logical array
logicalArray = logical(characterArray - '0')

サインインしてコメントする。

その他の回答 (3 件)

Steven Lord
Steven Lord 2020 年 8 月 28 日

1 投票

You can use implicit expansion.
>> r = randi(4, 10, 1); % Your values will vary
>> values = r == 1:4;
>> t = table(r, values)
t =
10×2 table
r values
_ __________________________
1 true false false false
2 false true false false
3 false false true false
4 false false false true
4 false false false true
1 true false false false
4 false false false true
4 false false false true
2 false true false false
4 false false false true
Anthony Bartoletti
Anthony Bartoletti 2019 年 10 月 14 日
編集済み: Anthony Bartoletti 2019 年 10 月 14 日

0 投票

I'm not sure this is the answer requested. The solution given turns
1 into ( 1 0 0 0 ), 2 into ( 0 1 0 0 ), 3 into ( 1 1 0 0 ), 4 into ( 0 0 1 0 ).
But for a classification problem, you want to turn
1 into ( 1 0 0 0 ), 2 into ( 0 1 0 0 ), 3 into ( 0 0 1 0 ), 4 into ( 0 0 0 1 ).
I managed a solution: Create a logical 4x4 identity matrix with I = logical(eye(4)). Then given a sequence (row) r of integers 1 <= n <= 4, conduct
M = I( :, r)
This will select from the 4 logical basis vectors as columns, according to the sequence r.
Deryck Chan
Deryck Chan 2020 年 8 月 28 日

0 投票

I've combined the two approaches above and came up with this rather elegant solution:
% Example input
y = [1 3 4 5 7 9 8 2 1 5 10 3 4 6 8 2 6 1 2 7 10];
num_labels = max(y)
% Convert y into y_matrix
logic_template = eye(num_labels);
y_matrix = logic_template(:, y);
You can use boolean(eye(num_labels)) instead of eye(num_labels) if you want the result to be in the logical datatype.

カテゴリ

ヘルプ センター および File Exchange で Data Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by