码迷,mamicode.com
首页 > 编程语言 > 详细

MATLAB--巧妙运用逻辑数组

时间:2017-11-14 19:31:50      阅读:395      评论:0      收藏:0      [点我收藏+]

标签:magic   com   逻辑   matlab   数组   tla   style   label   matrix   

测试数据:

>> yv = magic(4)
yv =

16     2     3    13
 5    11    10     8
 9     7     6    12
 4    14    15     1

>> y = [3;4;2;1]

y =

 3
 4
 2
 1

style 1

yv=[1:num_labels] == y

>> yv = [1:4] == y

yv =

4×4 logical 数组

0   0   1   0
0   0   0   1
0   1   0   0
1   0   0   0

style 2

yv = zeros(m, num_labels);
for i = 1:m
yv(i, y(i)) = 1;
end

>> yv = zeros(4,4)

yv =

     0     0     0     0
     0     0     0     0
     0     0     0     0
     0     0     0     0

>> for i = 1:4
yv(i, y(i)) = 1;
end

>> yv
     
yv =
     
    0     0     1     0
    0     0     0     1
    0     1     0     0
    1     0     0     0


style 3

all_combos = eye(num_labels);
y_matrix = all_combos(y,:)

>> all_combos = eye(4);
>> y_matrix = all_combos(y,:) 

y_matrix =

     0     0     1     0
     0     0     0     1
     0     1     0     0
     1     0     0     0

MATLAB--巧妙运用逻辑数组

标签:magic   com   逻辑   matlab   数组   tla   style   label   matrix   

原文地址:http://www.cnblogs.com/hello-world-n/p/7833982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!