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

1 visualizzazione (ultimi 30 giorni)
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

Risposte (1)

Jos (10584)
Jos (10584) il 25 Mar 2016
Modificato: Jos (10584) il 25 Mar 2016
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 ;

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by