How to apply efficient if-else statements in a big data cell array

조회 수: 2 (최근 30일)
Shima Asaadi
Shima Asaadi 2016년 3월 25일
편집: Jos (10584) 2016년 3월 25일
I have a cell array of 3 columns. Based on the different joint values of 2nd and 3rd columns, I would like to assign a number to the values of first column. for example:
a x z
b y z
c x y
d y z
for x&z I assign 1 to a, for y&z I assign 2 to b and d, for x&y I assign 3 to c. Well, I can do it within a for-loop and some if-else statements. But the document is too big and this is a bit slow. Does anyone know about an efficient and fast way to do it so?
Many thank, Shima

답변 (1개)

Jos (10584)
Jos (10584) 2016년 3월 25일
편집: Jos (10584) 2016년 3월 25일
It is a cell array, but each cell has a single number? Then you might be better of converting it to numbers first:
N = cell2mat(C) ;
N(N(:,2)==x & N(:,3)==z, 1) = 1 ;
N(N(:,2)==y & N(:,3)==z, 1) = 2 ;
N(N(:,2)==x & N(:,3)==y, 1) = 3 ;

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by