0

为了

A=[1;3;5]

B=cell(7,1)

我将以下结果存储在单元格中

[1]
[3]
[5]
[1;3]
[1;5]
[3;5]
[1;3;5]

我想以,和print的方式得到结果。-- 基本上将 A 中的每个值分配给一个变量。a=1b=3c=5

我将如何在 Matlab 中执行此操作?


我正在寻找可能是这样的结果:

" you can have a "
" you can have b "
" you can have c "
" you can have a or b "
.
.
etc
4

2 回答 2

1

如果我理解正确,你想要这样的东西:

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

您问题的主要部分是如何将每个数字分配给一个字母(注意:我知道您要求将每个数字分配给一个变量,但我认为这不是您想要的。)。这是使用一个名为 的查找表完成的numToLetter,该表已a存储在1b存储在3c存储在5。这样,您只需将输入的数字用作此表的索引。您可以将此查找表与向量一起使用;例如:

myNumbers = [ 1 3 3 1 5 ];
myLetters = numToLetter(myNumbers)

给出输出:

myLetters =

abbac
于 2013-03-06T09:14:20.540 回答
1

C成为您要分配给 中数字的字母数组A。然后

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 '), '_', '')];

结果是

str =

you can have a or b or c

如果要一次创建对所有字段的响应 ,可以使用B

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'

这段代码的逐步解释如下:

% 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];

要使用完整的单词(按照评论中的要求)进行此操作,您需要转换C为字符串元胞数组并相应地更新公式:

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
于 2013-03-06T09:54:03.417 回答