2009-11-03 86 views
4

我有三個陣列,所有具有相同的尺寸:MATLAB:從雙陣列矢量分配給單元陣列

xout  % cell array 
xin   % numeric array of doubles 
b   % logical array 

如何可以採取對應於其中b爲真索引鑫的元素,並分配他們到相應的地方在XOUT?

>> xout = {'foo', 'bar', 'baz', 'quux'}; 
>> xin = [1, 2, 3, 4]; 
>> b = (xin ~= 2);  % yields [1 0 1 1] in this case 
>> xout{b}=xin(b); 
??? The right hand side of this assignment has too few values 
to satisfy the left hand side. 

>> xout(b)=xin(b); 
??? Conversion to cell from double is not possible. 

回答

5

其分配給xout之前,您應該使用功能NUM2CELL右手邊轉換爲一個單元陣列:

xout(b) = num2cell(xin(b)); 
+0

衛生署!這工作,謝謝。 – 2009-11-03 20:35:34