Question on CATEGORICAL and Help files
3 次查看(过去 30 天)
显示 更早的评论
Well, not a question as much as a comment. I'm finding the Help files a bit too brief for this class. Does any one else agree?
For example:
- How to preallocate a categorical array? I'm dealing with 13,000,000 records and moving the data into variables in 32 bit machine hits the memory limit quickly. Preallocation is critical inthis application.
- How to turn a categorical array back into numbers of characters? Hehe, trying to use the variable editor and paste the categorical data into a column of another (non categorical) variable, froze the machine (or was it a crash?).
0 个评论
采纳的回答
Sean de Wolski
2014-8-20
I usually preallocate tables or categoricals dynamically by running my loop backward
T = table;
for ii = 10:-1:1;
T(ii,:) = array2table(rand(1,4));
end
To your second question:
c = categorical({'red';'red';'blue'})
cellstr(c)
9 个评论
dpb
2014-8-21
Both...I'm not at all enamored of the idea of changing the UI, either...I use very little of it other than keyboard, anyway.
Peter Perkins
2014-8-22
In addition to what Sean said about preallocating the memory using a backwards loop
- you can also assign any value to the last element and then run the loop in the usual direction, and
- it will be a performance gain to also preallocate the categories if you know them in advance.
So
c(1000,1) = categorical('',{'abc' 'def' 'ghi'});
Presto, a 1000x1 categorical array. You could of course also do this
c = categorical(repmat({''},1000,1),{'abc' 'def' 'ghi'});
but there's no real reason to.
更多回答(1 个)
dpb
2014-8-20
a) Can't -- nominal or ordinal create the categorical array from an existing array--no other method is provided.
b) double and various intXX are numeric conversions; cellstr or char for character data
See
doc categorical
for details.
If data are character, you may in the end save memory with such manipulations as
x=nominal(x);
if x is a character variable but you'll have to have the original x initially. If you're running into memory problems loading the data to begin with, about all you can do that I can think of is to load it piecemeal, convert to categorical with a (hopefully sizable) savings in memory that then allows you to load some more.
Or, of course, find a way to process the data other than "all in one swell foop"
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Categorical Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!