Effacer les filtres
Effacer les filtres

How to use if statement in cells

2 vues (au cours des 30 derniers jours)
Abirami
Abirami le 2 Fév 2015
Modifié(e) : Stephen23 le 19 Fév 2015
Hello I have doubts in using if condition in matlab.I have to perform the following operation in a cell.
Final={ S1 if B{i}=11
S1 if B{i}=10
S2 if B{i}=01
S2 if B{i}=00
I have the 2 bit binary values as a 128x17 cell. How to perform this and create a new matrix Z where i get the values of S1 and S2.
B= 00 10 11 10 11
01 01 01 01 11
10 00 01 00 01
10 10 01 01 11
10 10 10 00 10
and if
S1= 21
23
28
25
43
S2= 96
85
78
65
76
For each binary value, the corresponding S value should be present.So my Z would be
Z = s2 s1 s1 s1 s1
s2 s2 s2 s2 s1
s1 s2 s2 s2 s2
s1 s1 s2 s2 s1
s1 s1 s1 s2 s1
ie Z= 96 21 21 21 21
85 85 85 85 23
28 78 78 78 78
25 25 65 65 25
43 43 43 76 43
ie; with the values of S1 and S2, the data in B is to be replaced.please help. thanks in advance
  1 commentaire
Jan
Jan le 2 Fév 2015
What does this mean:
Final={ S1 if B{i}=11
S1 if B{i}=10
S2 if B{i}=01
S2 if B{i}=00
Using a personally invented syntax is useful if you explain the meaning. E.g. what is "01"? Is this a [1x2] CHAR vector or a [1x2] uint8 or logical vector? What are "2 bit binary values" and how do you store them? Where does the "128x17 cell" occur in your code? What type do S1 and S2 have?
Currently a reader have to guess to many details.

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 2 Fév 2015
Modifié(e) : Stephen23 le 2 Fév 2015
It is not clear exactly what format your "binary" data are in, I assumed that they are strings. This script creates the matrix Z as you indicate:
B = {'00','10','11','10','11';...
'01','01','01','01','11';...
'10','00','01','00','01';...
'10','10','01','01','11';...
'10','10','10','00','10'};
S1 = [21;23;28;25;43];
S2 = [96;85;78;65;76];
%
C = cellfun(@bin2dec,B)>1.5;
[r,~] = find(C);
Z(C) = S1(r);
[r,~] = find(~C);
Z(~C) = S2(r);
Z = reshape(Z,size(C))
When I run the script, it display this in my command window:
Z =
96 21 21 21 21
85 85 85 85 23
28 78 78 78 78
25 25 65 65 25
43 43 43 76 43
EDIT: For slightly more compact code, this is equivalent:
C = cellfun(@bin2dec,B)>1.5;
Z(C) = S1(1+rem(find(C)-1,size(C,1)));
Z(~C) = S2(1+rem(find(~C)-1,size(C,1)));
Z = reshape(Z,size(C))
  3 commentaires
Abirami
Abirami le 19 Fév 2015
Modifié(e) : Abirami le 19 Fév 2015
Hello sir, When i executed it for an even greater cell, the resulting cell had zeroes in the first row for values 10 and 11...Also why is '>1.5' used?Please help thanks in advance....
Stephen23
Stephen23 le 19 Fév 2015
Modifié(e) : Stephen23 le 19 Fév 2015
Which "resulting cell" do you mean? Do you mean the output Z, which is not a cell array, but rather a numeric array ? If this is the case, then please add a comment showing the input values that caused these zeros to occur. I can't read minds, and I don't know what your data looks like.
The code X>1.5 compares whatever compares X with 1.5, and returns a true value for every element of X is greater than 1.5. This is used to allocate the elements of input array B to either S1 or S2.

Connectez-vous pour commenter.

Plus de réponses (0)

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