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

2 vues (au cours des 30 derniers jours)
Shima Asaadi
Shima Asaadi le 25 Mar 2016
Modifié(e) : Jos (10584) le 25 Mar 2016
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

Réponses (1)

Jos (10584)
Jos (10584) le 25 Mar 2016
Modifié(e) : Jos (10584) le 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 ;

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by