2013-03-06 33 views
0

對於如何在Matlab中將單元格的值作爲變量打印出來?

A=[1;3;5] 

B=cell(7,1) 

我已經儲存在電池下面的結果

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

我想print的方式,結果是a=1b=3c=5。 - 基本上將A中的每個值分配給一個變量。

我該如何在Matlab中做到這一點?


我找它可以是這樣的結果:

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

回答

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 
+0

你能解釋一下你的代碼嗎?我真的不明白髮生了什麼,並且很想知道它是如何工作的。 – NLed 2013-03-06 11:10:54

+0

@NLed請參閱我的更新回答。 – 2013-03-06 13:08:37

+0

謝謝,讓我更有意義..'arrayfun(@x ..)'代表什麼? '@ x'有什麼用?對於第二個代碼,最後一部分,你有'(1:length(B))','uniformoutput',false)'......這是什麼意思? – NLed 2013-03-06 20:52:51

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的查找表完成的,其具有存儲在1a,b存儲在3和存儲在5c。通過這種方式,您可以簡單地將輸入數字用作表格中的索引。你可以使用這個查找表與向量;例如:

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

給人的輸出:

myLetters = 

abbac 
+0

我剛剛注意到你在'numToLetter(B {i}(j)'這裏做了轉換。但是你能解釋一下這是如何工作的嗎?我很想理解這一點,並且學習而不是複製。 – NLed 2013-03-06 11:19:22

+0

另外,爲什麼你在'numToLetter = ['a','','b','','c'];''字母之間?爲什麼不'numToLetter = ['a','b','c']'? – NLed 2013-03-06 11:20:48

+0

我已經嘗試過在同一個單元格上,但添加了幾個零,例如'1變成了100','3變成了300'等等......我得到這個錯誤:'索引超出了矩陣的維數。「爲什麼? – NLed 2013-03-06 12:06:25

相關問題