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:
TRANSLATE ONE VARIABLE INTO TWO

Subject: TRANSLATE ONE VARIABLE INTO TWO

From: Slawomir

Date: 17 Apr, 2012 14:52:07

Message: 1 of 4

Hello!

Let assume that we have only 2 kinds of integer numbers input data: 2 and 4:

a = [
2
2
2
2
4
4
4
2
4
.
.
.
n];

and I want to create from it sth like that:
d = [
0 0
0 0
0 0
0 0
1 1
1 1
1 1
0 0
1 1
.
.
.
n];

I want to translate n rows of data.

if one value in vector a = 2, i want it to be 0 0, and if another value equals 4 to be 1 1. I know how to do it but I don't know how to write a code.

%=================
a = [2;4;2;242;2;4;4;4;4;2];
[a b] = size(a)
i = 1;
for i <b
if b(i)=2
d = [what?]
else
d = [what?]
end
i = i + 1;
end
d
%=================

How to create the output vector that will save in correct order the data from vector a?

Thanks for help!

Subject: TRANSLATE ONE VARIABLE INTO TWO

From: someone

Date: 17 Apr, 2012 15:16:07

Message: 2 of 4

"Slawomir" wrote in message <jmk02n$8ba$1@newscl01ah.mathworks.com>...
> Hello!
>
> Let assume that we have only 2 kinds of integer numbers input data: 2 and 4:
>
> a = [
> 2
> 2
> 2
> 2
> 4
> 4
> 4
> 2
> 4
> .
> .
> .
> n];
>
> and I want to create from it sth like that:
> d = [
> 0 0
> 0 0
> 0 0
> 0 0
> 1 1
> 1 1
> 1 1
> 0 0
> 1 1
> .
> .
> .
> n];
>
> I want to translate n rows of data.
>
> if one value in vector a = 2, i want it to be 0 0, and if another value equals 4 to be 1 1. I know how to do it but I don't know how to write a code.
>
> %=================
> a = [2;4;2;242;2;4;4;4;4;2];
> [a b] = size(a)
> i = 1;
> for i <b
> if b(i)=2
> d = [what?]
> else
> d = [what?]
> end
> i = i + 1;
> end
> d
> %=================
>
> How to create the output vector that will save in correct order the data from vector a?
>
> Thanks for help!

% One method:

a = [2;4;2;2;4;2;2;4;4;4;4;2];
c = a./2 - 1;
ncols = 2; % number of columns
d = repmat(c,1,ncols)

Subject: TRANSLATE ONE VARIABLE INTO TWO

From: Roger Stafford

Date: 17 Apr, 2012 15:29:08

Message: 3 of 4

"Slawomir" wrote in message <jmk02n$8ba$1@newscl01ah.mathworks.com>...
> a = [2;4;2;242;2;4;4;4;4;2];
> [a b] = size(a)
> i = 1;
> for i <b
> if b(i)=2
> d = [what?]
> else
> d = [what?]
> end
> i = i + 1;
> end
> d
- - - - - - - - -
  How about

 d = [a/2-1,a/2-];

  However if you wish to use a for-loop and the if-else construct, it would be something like this:

 for k = 1:size(a,1)
  if a(k) == 2
   d(k,:) = [0,0];
  else
   d(k,:) = [1,1];
  end
 end

  I would advise you to read the pertinent documentation on this carefully. The for-loop index should not be altered as you have done with "i=i+1" since that is automatically done. Also "b(i)=2" is not correct. Use "==" for equality tests.

Roger Stafford

Subject: TRANSLATE ONE VARIABLE INTO TWO

From: Slawomir

Date: 17 Apr, 2012 17:14:07

Message: 4 of 4

Thank you, guys!
@Robert, you're right, need to practice the loops. All of us started somewhere. :)

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