MATLAB Fundamentals>>Representing Discrete Categories

发布时间:2024年01月18日

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后面部分可以不使用括号,执行运算顺序为从又向左,即先计算“等于”或“不等于”逻辑,在将结果赋值给左边变量。

文章来源:https://blog.csdn.net/syluxhch/article/details/135646414
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。