سؤال

لدي ثلاثة صفائف، كل نفس الحجم:

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

كيف يمكنني أخذ عناصر XIN التي تتوافق مع المؤشرات التي يكون فيها 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.
هل كانت مفيدة؟

المحلول

يجب عليك استخدام الوظيفة num2cell لتحويل الجانب الأيمن إلى صفيف خلية قبل تعيينه xout:

xout(b) = num2cell(xin(b));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top