MATLAB Fundamentals>>Representing Discrete Categories

MATLAB Fundamentals>Specialized Data Types>Representing Discrete Categories>(1/6) Introduction

Representing Discrete Categories

When text labels are intended to represent a finite set of possibilities, a string array may use more memory than a categorical array. There are functions designed to work with categories of data, so if your text describes a group or category, this is often the best data type.
当文本标签旨在表示一组有限的可能性时,字符串数组可能比分类数组使用更多的内存。有些函数是为处理数据类别而设计的,因此,如果您的文本描述了一个组或类别,这通常是最好的数据类型

MATLAB Fundamentals>Specialized Data Types>Representing Discrete Categories>(2/6) Converting to and Operating on Categoricals

This code sets up the activity.

x = ["C","B","C","A","B","A","C"]

This line of code would produce an error.

% histogram(x)

说明1:You can convert a string array to a categorical using the categorical function.

任务1:Convert x into a categorical array named y.

解答:

y = categorical(x)

说明2:Categorical arrays take up less memory than cell arrays of strings.

任务2:Issue the whos x y command to see how much memory each variable uses.

解答:

whos x y

任务3:

You can see the categories represented in a categorical array using the categories function on the categorical array.

解答:

cats = categories(y)

任务4:

Create a variable named iC which contains values of true corresponding to the values in y that equal C.

解答:

iC = y == "C"

任务5:

Create a variable named iNB which contains values of true corresponding to the values in y that are not equal to B.

解答:

iNB = y ~= "B"

附加练习:Try indexing into y with iC and iNB.

解答:

iCidx = y(iC)
iNBidx = y(iNB)

笔记:

(1)a string array may use more memory than a categorical array字符串数组比分类数组占用更多内存;

(2)任务4和5后面部分可以不使用括号,执行运算顺序为从又向左,即先计算“等于”或“不等于”逻辑,在将结果赋值给左边变量。

  • 16
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值