Arrays 如何在Matlab中将值打印为单元格中的变量?
为了 及 我将以下结果存储在一个单元格中Arrays 如何在Matlab中将值打印为单元格中的变量?,arrays,matlab,cell,Arrays,Matlab,Cell,为了 及 我将以下结果存储在一个单元格中 B=cell(7,1) 我想以a=1、b=3和c=5的方式打印结果基本上,将A中的每个值分配给一个变量 如何在Matlab中实现这一点 我正在寻找一个类似这样的结果: [1] [3] [5] [1;3] [1;5] [3;5] [1;3;5] 如果我理解正确,你想要这样的东西: " you can have a " " you can have b " " you can have c " " you can have a or b " . . e
B=cell(7,1)
我想以a=1
、b=3
和c=5
的方式打印结果基本上,将A中的每个值分配给一个变量
如何在Matlab中实现这一点
我正在寻找一个类似这样的结果:
[1]
[3]
[5]
[1;3]
[1;5]
[3;5]
[1;3;5]
如果我理解正确,你想要这样的东西:
" you can have a "
" you can have b "
" you can have c "
" you can have a or b "
.
.
etc
你的主要问题是如何将每个数字分配给一个字母(注意:我知道你要求将每个数字分配给一个变量,但我认为这不是你想要的。)。这是使用一个名为numToLetter
的查找表来完成的,该表将a
存储在1
,b
存储在3
,以及c
存储在5
。这样,您只需将输入的数字用作此表的索引。您可以将此查找表与向量一起使用;例如:
numToLetter = [ 'a', ' ', 'b', ' ', 'c' ];
B = { 1, 3, 5, [ 1; 3 ], [ 1; 5 ], [ 3; 5 ], [ 1; 3; 5 ] };
% Loop though each entry in our cell array
for i = 1 : length(B)
fprintf(' you can have '); % Print initial message
% Loop though each vector element inside the B{i}
for j = 1 : length(B{i})
fprintf('%c', numToLetter(B{i}(j) ) ) % Use our numToLetter lookup table
% to convert the number to a letter,
% and print it out.
if j ~= length(B{i})
fprintf(' or '); % Print 'or' if there are more to come
end
end
fprintf('\n'); % New line
end
给出输出:
myNumbers = [ 1 3 3 1 5 ];
myLetters = numToLetter(myNumbers)
让
C
成为要分配给A
中的数字的字母数组。然后
myLetters =
abbac
导致
A = [1 3 5];
B = {[1]; [3]; [5]; [1;3]; [1;5]; [3;5]; [1;3;5]};
C = ['a', 'b', 'c']
k = 6; % indicates current line of B
str = ['you can have ' strrep(strrep(sprintf('_%c_', ...
C(ismember(A, B{k}))'), '__', ' or '), '_', '')];
如果要同时创建对B
中所有字段的响应,可以使用
str =
you can have a or b or c
这导致
allStr = arrayfun(@(x) ['you can have ' strrep(strrep(sprintf('_%c_', ...
C(ismember(A, B{x}))'), '__', ' or '), '_', '')], ...
(1:length(B))', 'uniformoutput', false)
此代码的逐步解释如下所示:
allStr =
'you can have a'
'you can have b'
'you can have c'
'you can have a or b'
'you can have a or c'
'you can have b or c'
'you can have a or b or c'
要使用完整的单词(如注释中所要求的),您需要将C
转换为字符串单元格数组,并相应地更新公式:
% which contents of A can be found in B?
idx = ismember(A, B{k})';
% to which letters do these indices correspond?
letters = C(idx);
% group the letters in a string embedded in '_' as place holders for later use
% by this, the places between letters will be marked with '__' and the places
% at the beginning and the end of the string will be marked with '_'
stringRaw = sprintf('_%c_', letters);
% replace each occurrence of '__' by ' or '
stringOr = strrep(stringRaw, '__', ' or ');
% replace each occurrence of '_' by ''
stringClean = strrep(stringOr, '_', '');
% add first half of sentence
stringComplete = ['you can have ' stringClean];
这导致:
A = [1 3 5];
B = {[1]; [3]; [5]; [1;3]; [1;5]; [3;5]; [1;3;5]};
C = {'first', 'second', 'third'}
k = 7; % indicates current line of B
str = ['you can have ' strrep(strrep(sprintf('_%s_', ...
C{ismember(A, B{k})}), '__', ' or '), '_', '')];
你能解释一下你的密码吗?我真的不明白发生了什么,我很想知道它是如何工作的。谢谢你,这有点道理。。
arrayfun(@x..)
代表什么?@x
有什么用?同样对于第二个代码,最后一部分,您有(1:length(B)),'uniformoutput',false)
。。这是什么意思?此外,我如何才能将名称更改为其他名称?如果我先将其更改为而不是一个变量a
,则a b c
将替换为f I r
。我怎样才能解决这个问题?我用一个%s
替换了%c
,但是没有用。谢谢您的回答,我非常感谢。关于arrayfun
,请参阅。第二个问题我现在无法回答,因为我无法使用matlab。提示:尝试将C
转换为单元格数组,并相应地更新其他公式。我提出了一个关于将这些打印为字符串的新问题,因为您给我的答案非常适合打印为变量。我刚刚注意到您在numToLetter(B{I}(j)处进行转换
。但是你能解释一下这是如何工作的吗?我想了解这一点并学习,而不仅仅是抄袭。还有,为什么你在字母之间用空格隔开numToLetter=['a'、''b'、''c'];
为什么不numToLetter=['a'、'b'、'c']
?我在同一个单元格上尝试过这个方法,但添加了一些零,例如1变为100
和3变为300
等等。我得到了这个错误:索引超过了矩阵维数。
为什么?仔细看我答案的底部,这应该可以解释你的问题。我之所以留下空格是因为它们位于正数ons 2和ons 4,您没有使用。我需要在numToLetter
数组中分别将'a'、'b'和'c'置于位置1、3和5,因为您要转换的数字是数组中的索引。一旦您了解我使用查询编号作为numToLetter
数组中的索引,您应该了解如何转换修改它。希望有帮助。哦,那么代码只解释那些特定的数字(13 5
)?我宁愿有一些可以适应任何数字的东西,比如100、300或500(以及任何其他数字)…你知道我该怎么做吗?
A = [1 3 5];
B = {[1]; [3]; [5]; [1;3]; [1;5]; [3;5]; [1;3;5]};
C = {'first', 'second', 'third'}
k = 7; % indicates current line of B
str = ['you can have ' strrep(strrep(sprintf('_%s_', ...
C{ismember(A, B{k})}), '__', ' or '), '_', '')];
str =
you can have first or second or third