Add Data to a file during each iteration

  • Follow


Hi All;
I am new to programming word. I am trying to run a "for loop" to find
the binary equivalent of decimal numbers. I want to write the binary
number generated during each iteration in a separate row of an array.
Please tell me how can I do the same. The for loop detail are given
below:

for i=1:y
bin = dec2bin(i)
alldata(i,:)=bin   %this one is not working for above function,
altough I am able to use it for simple iteration
end

Any help will be appreciated.
Thanks
Hakuna Matata
0
Reply Hakuna 9/10/2010 10:17:22 AM

Hakuna Matata <hirdeshs8@gmail.com> wrote in message <ae2399de-574d-42d0-bba5-4a65a30f1328@k1g2000prl.googlegroups.com>...
> Hi All;
> I am new to programming word. I am trying to run a "for loop" to find
> the binary equivalent of decimal numbers. I want to write the binary
> number generated during each iteration in a separate row of an array.
> Please tell me how can I do the same. The for loop detail are given
> below:
> 
> for i=1:y
> bin = dec2bin(i)
> alldata(i,:)=bin   %this one is not working for above function,
> altough I am able to use it for simple iteration
> end
> 
> Any help will be appreciated.
> Thanks
> Hakuna Matata

Hi, your problem is that dec2bin returns a binary string so the size of the element is actually changing. For example, dec2bin(1) is a 1x1 char, but dec2bin(2) is a 1x2 char. One way is to use a cell array:

C=cell(10,1);
for i=1:10 C{i}=dec2bin(i); 
end

% get the last element of C and convert back to char--equal to dec2bin(10)
char(C{end})

It wasn't clear to me from your post, whether you wanted to write the same entry dec2bin(i) across multiple columns, or you had in mind an array with a single column.

Wayne
0
Reply Wayne 9/10/2010 11:43:04 AM


Hakuna Matata <hirdeshs8@gmail.com> wrote in message <ae2399de-574d-42d0-bba5-4a65a30f1328@k1g2000prl.googlegroups.com>...
> Hi All;
> I am new to programming word. I am trying to run a "for loop" to find
> the binary equivalent of decimal numbers. I want to write the binary
> number generated during each iteration in a separate row of an array.
> Please tell me how can I do the same. The for loop detail are given
> below:
> 
> for i=1:y
> bin = dec2bin(i)
> alldata(i,:)=bin   %this one is not working for above function,
> altough I am able to use it for simple iteration
> end
> 
> Any help will be appreciated.
> Thanks
> Hakuna Matata

you are nearly there - try 

alldata(i)=bin

bin is just a scalar, so you just needa single location for it.

Ross
0
Reply Ross 9/10/2010 11:44:04 AM

"Ross W" <rosswoodskiwi@hotmail.com> wrote in message <i6d5m4$rqn$1@fred.mathworks.com>...
> Hakuna Matata <hirdeshs8@gmail.com> wrote in message <ae2399de-574d-42d0-bba5-4a65a30f1328@k1g2000prl.googlegroups.com>...
> > Hi All;
> > I am new to programming word. I am trying to run a "for loop" to find
> > the binary equivalent of decimal numbers. I want to write the binary
> > number generated during each iteration in a separate row of an array.
> > Please tell me how can I do the same. The for loop detail are given
> > below:
> > 
> > for i=1:y
> > bin = dec2bin(i)
> > alldata(i,:)=bin   %this one is not working for above function,
> > altough I am able to use it for simple iteration
> > end
> > 
> > Any help will be appreciated.
> > Thanks
> > Hakuna Matata
> 
> you are nearly there - try 
> 
> alldata(i)=bin
> 
> bin is just a scalar, so you just needa single location for it.
> 
> Ross

Hi Ross, I don't think this will work for the OP because dec2bin() does not return a scalar. It returns a character array. Note that

length(dec2bin(2))

 is not equal to:

length(dec2bin(10))

Wayne
0
Reply Wayne 9/10/2010 12:12:04 PM


"Wayne King" <wmkingty@gmail.com> wrote in message 
news:i6d5k8$o2p$1@fred.mathworks.com...
> Hakuna Matata <hirdeshs8@gmail.com> wrote in message 
> <ae2399de-574d-42d0-bba5-4a65a30f1328@k1g2000prl.googlegroups.com>...
>> Hi All;
>> I am new to programming word. I am trying to run a "for loop" to find
>> the binary equivalent of decimal numbers. I want to write the binary
>> number generated during each iteration in a separate row of an array.
>> Please tell me how can I do the same. The for loop detail are given
>> below:
>>
>> for i=1:y
>> bin = dec2bin(i)
>> alldata(i,:)=bin   %this one is not working for above function,
>> altough I am able to use it for simple iteration
>> end
>>
>> Any help will be appreciated.
>> Thanks
>> Hakuna Matata
>
> Hi, your problem is that dec2bin returns a binary string so the size of 
> the element is actually changing. For example, dec2bin(1) is a 1x1 char, 
> but dec2bin(2) is a 1x2 char. One way is to use a cell array:
>
> C=cell(10,1);
> for i=1:10 C{i}=dec2bin(i); end

That's the approach I'd probably use, but if the OP really needs a matrix of 
characters for whatever reason, I would recommend using the second input 
argument to DEC2BIN to force DEC2BIN to return a representation with at 
least N binary digits; choose N sufficiently large that the maximum value 
being converted (in this case y) only requires N binary digits to be 
represented (use NEXTPOW2 to determine N if necessary.)

-- 
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on 
http://www.mathworks.com 

0
Reply Steven_Lord 9/10/2010 1:33:11 PM

"Wayne King" <wmkingty@gmail.com> wrote in message <i6d7ak$f3f$1@fred.mathworks.com>...
> "Ross W" <rosswoodskiwi@hotmail.com> wrote in message <i6d5m4$rqn$1@fred.mathworks.com>...
> > Hakuna Matata <hirdeshs8@gmail.com> wrote in message <ae2399de-574d-42d0-bba5-4a65a30f1328@k1g2000prl.googlegroups.com>...
> > > Hi All;
> > > I am new to programming word. I am trying to run a "for loop" to find
> > > the binary equivalent of decimal numbers. I want to write the binary
> > > number generated during each iteration in a separate row of an array.
> > > Please tell me how can I do the same. The for loop detail are given
> > > below:
> > > 
> > > for i=1:y
> > > bin = dec2bin(i)
> > > alldata(i,:)=bin   %this one is not working for above function,
> > > altough I am able to use it for simple iteration
> > > end
> > > 
> > > Any help will be appreciated.
> > > Thanks
> > > Hakuna Matata
> > 
> > you are nearly there - try 
> > 
> > alldata(i)=bin
> > 
> > bin is just a scalar, so you just needa single location for it.
> > 
> > Ross
> 
> Hi Ross, I don't think this will work for the OP because dec2bin() does not return a scalar. It returns a character array. Note that
> 
> length(dec2bin(2))
> 
>  is not equal to:
> 
> length(dec2bin(10))
> 
> Wayne

you're right wayne - sorry OP. I'm obviously still not quite with it after the earthquake!
0
Reply Ross 9/11/2010 1:09:05 AM

Thanks buddies,
It worked.. I got it

Regards
Hirdesh


On Sep 11, 6:09=A0am, "Ross W" <rosswoodsk...@hotmail.com> wrote:
> "Wayne King" <wmkin...@gmail.com> wrote in message <i6d7ak$f3...@fred.mat=
hworks.com>...
> > "Ross W" <rosswoodsk...@hotmail.com> wrote in message <i6d5m4$rq...@fre=
d.mathworks.com>...
> > >HakunaMatata<hirdes...@gmail.com> wrote in message <ae2399de-574d-42d0=
-bba5-4a65a30f1...@k1g2000prl.googlegroups.com>...
> > > > Hi All;
> > > > I am new to programming word. I am trying to run a "for loop" to fi=
nd
> > > > the binary equivalent of decimal numbers. I want to write the binar=
y
> > > > number generated during each iteration in a separate row of an arra=
y.
> > > > Please tell me how can I do the same. The for loop detail are given
> > > > below:
>
> > > > for i=3D1:y
> > > > bin =3D dec2bin(i)
> > > > alldata(i,:)=3Dbin =A0 %this one is not working for above function,
> > > > altough I am able to use it for simple iteration
> > > > end
>
> > > > Any help will be appreciated.
> > > > Thanks
> > > >HakunaMatata
>
> > > you are nearly there - try
>
> > > alldata(i)=3Dbin
>
> > > bin is just a scalar, so you just needa single location for it.
>
> > > Ross
>
> > Hi Ross, I don't think this will work for the OP because dec2bin() does=
 not return a scalar. It returns a character array. Note that
>
> > length(dec2bin(2))
>
> > =A0is not equal to:
>
> > length(dec2bin(10))
>
> > Wayne
>
> you're right wayne - sorry OP. I'm obviously still not quite with it afte=
r the earthquake!

Thanks,
0
Reply hirdeshs8 (4) 9/14/2010 11:13:58 AM

6 Replies
342 Views

(page loaded in 0.402 seconds)

Similiar Articles:













7/22/2012 5:53:03 AM


Reply: