Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
a problem of “if…else” statement

Subject: a problem of “if…else” statement

From: Yihui

Date: 13 Apr, 2011 10:06:04

Message: 1 of 6

Hello,

I have a problem of “if…else” statement.
For instance, I have a data set bellow.
A B C D E F G
1 2 7 3 9
2 1 4 6 10
1 2 7 9 11



A B C D E F G
1 2 7 3 9 7 9
2 1 4 6 10 6 4
1 2 7 9 11 7 11
  
I want first data set to turn into second data set. There are complex conditions to determine the column F and column G.
Condition 1: numbers in column A determines whether column F or column G is filled with the number in column C. That is, if column A =1, then fills column F with numbers in column C; otherwise, fills column G with numbers in column C.
Ex1: when A1=1, F1=C1=7;
Ex2: when A2=2, G2=C2=4
After condition 1 is satisfied, either column F or column G is left empty, and then do condition 2.
Condition 2: If numbers in column B =1, then fill the empty column (F or G) with numbers in column D; otherwise, fill the empty column (F or G) with numbers in column E.
Ex1: when (condition1) A1=1 and when (condition2) B1=2, G1=E1=9;
Ex2: when (condition1) A2=2 and when (condition2) B2=1, F1=D1=6;
Can someone tell me how to do this? Thanks!
Sincerely,
Yihui

Subject: a problem of “if…else” statement

From: Nasser M. Abbasi

Date: 13 Apr, 2011 10:54:11

Message: 2 of 6

On 4/13/2011 3:06 AM, Yihui wrote:
> Hello,
>
> I have a problem of“if…else” statement.
> For instance, I have a data set bellow.
> A B C D E F G
> 1 2 7 3 9
> 2 1 4 6 10
> 1 2 7 9 11
>
>
>
> A B C D E F G
> 1 2 7 3 9 7 9
> 2 1 4 6 10 6 4
> 1 2 7 9 11 7 11
>
> I want first data set to turn into second data set. There are complex conditions to
> determine the column F and column G.
> Condition 1: numbers in column A determines whether column F or column G is filled
> with the number in column C. That is, if column A =1, then fills column F with
> numbers in column C; otherwise, fills column G with numbers in column C.
> Ex1: when A1=1, F1=C1=7;
> Ex2: when A2=2, G2=C2=4
> After condition 1 is satisfied, either column F or column G is left empty, and
> then do condition 2.
> Condition 2: If numbers in column B =1, then fill the empty column (F or G)
> with numbers in column D; otherwise, fill the empty column (F or G) with numbers in column E.
> Ex1: when (condition1) A1=1 and when (condition2) B1=2, G1=E1=9;
> Ex2: when (condition1) A2=2 and when (condition2) B2=1, F1=D1=6;
> Can someone tell me how to do this? Thanks!
> Sincerely,
> Yihui

Will your A and B columns always be such that if one had a '1' in some row,
then the other will have a '2' in the same row and vis versa?

If so, then a solution is a simple application of find:

-----------------
clear all
A=1; B=2; C=3; D=4; E=5; F=6; G=7;

%input is X matrix
X=[1 2 7 3 9 0 0;
    2 1 4 6 10 0 0;
    1 2 7 9 11 0 0];

%cond 1
X(X(:,A)==1,F) = X(X(:,A)==1,C);
X(X(:,A)==2,G) = X(X(:,A)==2,C);

%cond 2
X(X(:,B)==1,F)=X(X(:,B)==1,D);
X(X(:,B)==2,G)=X(X(:,B)==2,E);
-----------------------

X =

      1 2 7 3 9 7 9
      2 1 4 6 10 6 4
      1 2 7 9 11 7 11


If not, then an extra logic can be added. But before I do
that, lets first see if that is even needed.

--Nasser

Subject: a problem of “if…else” statement

From: Roger Stafford

Date: 13 Apr, 2011 18:38:06

Message: 3 of 6

"Yihui" wrote in message <io3sic$9kk$1@fred.mathworks.com>...
> Hello,
>
> I have a problem of “if…else” statement.
> For instance, I have a data set bellow.
> A B C D E F G
> 1 2 7 3 9
> 2 1 4 6 10
> 1 2 7 9 11
>
>
>
> A B C D E F G
> 1 2 7 3 9 7 9
> 2 1 4 6 10 6 4
> 1 2 7 9 11 7 11
>
> I want first data set to turn into second data set. There are complex conditions to determine the column F and column G.
> Condition 1: numbers in column A determines whether column F or column G is filled with the number in column C. That is, if column A =1, then fills column F with numbers in column C; otherwise, fills column G with numbers in column C.
> Ex1: when A1=1, F1=C1=7;
> Ex2: when A2=2, G2=C2=4
> After condition 1 is satisfied, either column F or column G is left empty, and then do condition 2.
> Condition 2: If numbers in column B =1, then fill the empty column (F or G) with numbers in column D; otherwise, fill the empty column (F or G) with numbers in column E.
> Ex1: when (condition1) A1=1 and when (condition2) B1=2, G1=E1=9;
> Ex2: when (condition1) A2=2 and when (condition2) B2=1, F1=D1=6;
> Can someone tell me how to do this? Thanks!
> Sincerely,
> Yihui
- - - - - - - - -
  Call your data array M.

t1 = M(:,1)==1);
t2 = ~t1;
t3 = M(:,2)==1);
s = M(:,4).*t3+M(:,5).*(~t3);
M(:,6) = M(:,3).*t1+s.*t2;
M(:,7) = M(:,3).*t2+s.*t1;

Roger Stafford

Subject: a problem of &#8220;if&#8230;else&#8221; statement

From: Roger Stafford

Date: 13 Apr, 2011 19:31:05

Message: 4 of 6

"Roger Stafford" wrote in message <io4qie$erk$1@fred.mathworks.com>...
> ......
> t1 = M(:,1)==1);
> t3 = M(:,2)==1);
> .......
- - - - - - -
  Correction: There were a couple of unwanted parentheses above.

t1 = M(:,1)==1;
t3 = M(:,2)==1;

Roger Stafford

Subject: a problem of &#8220;if&#8230;else&#8221; statement

From: Yihui

Date: 14 Apr, 2011 02:12:14

Message: 5 of 6

Dear Nasser,

Your resolution works very well. Thank you so much.

Sincerely,
Yihui

"Nasser M. Abbasi" <nma@12000.org> wrote in message <io3vcr$tp7$1@speranza.aioe.org>...
> On 4/13/2011 3:06 AM, Yihui wrote:
> > Hello,
> >
> > I have a problem of“if…else” statement.
> > For instance, I have a data set bellow.
> > A B C D E F G
> > 1 2 7 3 9
> > 2 1 4 6 10
> > 1 2 7 9 11
> >
> >
> >
> > A B C D E F G
> > 1 2 7 3 9 7 9
> > 2 1 4 6 10 6 4
> > 1 2 7 9 11 7 11
> >
> > I want first data set to turn into second data set. There are complex conditions to
> > determine the column F and column G.
> > Condition 1: numbers in column A determines whether column F or column G is filled
> > with the number in column C. That is, if column A =1, then fills column F with
> > numbers in column C; otherwise, fills column G with numbers in column C.
> > Ex1: when A1=1, F1=C1=7;
> > Ex2: when A2=2, G2=C2=4
> > After condition 1 is satisfied, either column F or column G is left empty, and
> > then do condition 2.
> > Condition 2: If numbers in column B =1, then fill the empty column (F or G)
> > with numbers in column D; otherwise, fill the empty column (F or G) with numbers in column E.
> > Ex1: when (condition1) A1=1 and when (condition2) B1=2, G1=E1=9;
> > Ex2: when (condition1) A2=2 and when (condition2) B2=1, F1=D1=6;
> > Can someone tell me how to do this? Thanks!
> > Sincerely,
> > Yihui
>
> Will your A and B columns always be such that if one had a '1' in some row,
> then the other will have a '2' in the same row and vis versa?
>
> If so, then a solution is a simple application of find:
>
> -----------------
> clear all
> A=1; B=2; C=3; D=4; E=5; F=6; G=7;
>
> %input is X matrix
> X=[1 2 7 3 9 0 0;
> 2 1 4 6 10 0 0;
> 1 2 7 9 11 0 0];
>
> %cond 1
> X(X(:,A)==1,F) = X(X(:,A)==1,C);
> X(X(:,A)==2,G) = X(X(:,A)==2,C);
>
> %cond 2
> X(X(:,B)==1,F)=X(X(:,B)==1,D);
> X(X(:,B)==2,G)=X(X(:,B)==2,E);
> -----------------------
>
> X =
>
> 1 2 7 3 9 7 9
> 2 1 4 6 10 6 4
> 1 2 7 9 11 7 11
>
>
> If not, then an extra logic can be added. But before I do
> that, lets first see if that is even needed.
>
> --Nasser
>
>
>

Subject: a problem of &#8220;if&#8230;else&#8221; statement

From: Yihui

Date: 14 Apr, 2011 02:13:04

Message: 6 of 6

Dear Stafford,

This works elegantly. Thank you very much.

Sincerely,
Yihui


"Roger Stafford" wrote in message <io4tlp$f5b$1@fred.mathworks.com>...
> "Roger Stafford" wrote in message <io4qie$erk$1@fred.mathworks.com>...
> > ......
> > t1 = M(:,1)==1);
> > t3 = M(:,2)==1);
> > .......
> - - - - - - -
> Correction: There were a couple of unwanted parentheses above.
>
> t1 = M(:,1)==1;
> t3 = M(:,2)==1;
>
> Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us