使用hashtable MATLAB创建矩阵
我正在MATLAB中使用哈希表(使用hashtable MATLAB创建矩阵,matlab,containers,Matlab,Containers,我正在MATLAB中使用哈希表(containers.map),现在我想用这些信息创建一个矩阵。因此,当我运行哈希表,然后在命令窗口中插入每种类型的文本文件时,类似于: m =[1 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 1 0 0 1]; 编辑 F = listdlg('Pr
containers.map
),现在我想用这些信息创建一个矩阵。因此,当我运行哈希表,然后在命令窗口中插入每种类型的文本文件时,类似于:
m =[1 0 0
1 0 0
1 0 0
0 1 0
0 1 0
0 1 0
0 1 0
0 1 0
0 1 0
0 0 1
0 0 1
0 0 1
0 0 1];
编辑
F = listdlg('PromptString','Different types', 'SelectionMode',...
'single', 'ListString',E, 'Name','Select a type','ListSize',[230 130]);
[files,path] = uigetfile ('*.txt','Select your text files',...
'MultiSelect','on');
其中E
只是用户的输入,在本例中为粉色
、紫色
和黄色
%save for each type the user enters the corresponding text files he
%wants to train
%A Map object is a data structure that allows you to retrieve values
%using a corresponding key. Keys can be real numbers or text strings
%and provide more flexibility for data access than array indices,
%which must be positive integers. Values can be scalar or nonscalar arrays.
handles.map (E(F,:)) = files;
handles.map
a = handles.map.values
b = handles.map.keys
handles.map.size
b =
{1x3 cell} {1x6 cell} {1x4 cell}
a =
'pink ' 'purple' 'yellow'
现在我想把b
的总数作为矩阵m
上的行数;因此,行14的总数和a
中的每一位成为一列;总共有3列。但我想创建一个二进制矩阵,其中每列将标识不同的类型。最后,我将创建这样一个矩阵:
m =[1 0 0
1 0 0
1 0 0
0 1 0
0 1 0
0 1 0
0 1 0
0 1 0
0 1 0
0 0 1
0 0 1
0 0 1
0 0 1];
其中,矩阵的前3行表示有3个类型为<代码>粉色<代码>的文本文件,接下来的6行是<代码>紫色<代码>类型的6个文本文件,最后4行是<代码>黄色<代码>类型的4:4个文本文件
%save for each type the user enters the corresponding text files he
%wants to train
%A Map object is a data structure that allows you to retrieve values
%using a corresponding key. Keys can be real numbers or text strings
%and provide more flexibility for data access than array indices,
%which must be positive integers. Values can be scalar or nonscalar arrays.
handles.map (E(F,:)) = files;
handles.map
a = handles.map.values
b = handles.map.keys
handles.map.size
b =
{1x3 cell} {1x6 cell} {1x4 cell}
a =
'pink ' 'purple' 'yellow'
我希望现在情况更清楚了。:)
任何帮助都将不胜感激!:)那么像这样的事情
val = cellfun(@length, b)';
m = 0;
for v = 1:size(val)
m(end:end+val(v)-1,v) = 1;
end
还不完全清楚,;您能更详细地解释一下地图中的键和值是什么吗?如果您能创建一个最小的工作示例,在其中用值填充映射,然后解释您想要的输出,那将是最好的。谢谢。。我刚刚编辑了它,我希望我现在更清楚:)@Chrysovalando等等。。。我以为你在说
b
是你的输入,m
是你想要的输出?不是这样吗是的是这样的。。但是当我在代码中使用它时,我只得到一列1,但如果我按原样运行它,我就得到了我想要的。。所以我的代码可能有点疯狂:/请看我原来的帖子:)@Chrysovalando尝试转置我原来的val
calc,看我的编辑