2013-03-21 28 views
-1

我有以下代碼:如何在matlab中使用'或'來選擇兩個值?

a = cell(4,1); 
a{1} = [5 3 0 0]; 
a{2} = [0 3 5 0]; 
a{3} = [1 3 0 0]; 
a{4} = [0 3 2 0]; 

arrayind = 2; 

b = a(cellfun(@(x)x(arrayind) == 1,a)); 
b{:} 

我怎樣才能做到這一點時,使用IF語句:

if r>2 
b = a(cellfun(@(x)x(arrayind) == (1 | 2 | 3),a)); 
end 

基本上說,發現1,如果不是有那麼2,如果不是有那麼3 ...

回答

0

ismember也許可能是你在找什麼。 與ismember更換平等操作如下:

a = cell(4,1); 
a{1} = [5 3 0 0]; 
a{2} = [0 3 5 0]; 
a{3} = [1 3 0 0]; 
a{4} = [0 3 2 0]; 
arrayind = 1; 
b = a(cellfun(@(x) ismember(x(arrayind), [1 5]), a)); 

會產生b = a([1, 3])

+0

你的代碼真的幫了我,但即時通訊面臨的一個新問題。如果你有時間,請檢查了這一點http://stackoverflow.com/questions/15576345/how-to-choose-identical-values-in-a-cell-array-using-matlab – NLed 2013-03-22 17:56:06

相關問題