Apply multiplication to all elements in an array

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Apply multiplication to all elements in an array

wei.jian
Hi there,

I'm new to coding and can someone guide or direct me to how I can multiply a
constant (say 5) to an array of elements:

For e.g. a = newArray(0,1,2,3,4);

I wish to get another array whereby: b = 0, 5, 10, 15, 20

Thanks.
WJ



--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Apply multiplication to all elements in an array

Krs5
Dear WJ,

Try

a = newArray(0,1,2,3,4);
b = newArray(5);
for (i=0; i<a.length; i++)
        b[i] = a[i]*5;
Array.print(b);

Best wishes

Kees


-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of wei.jian
Sent: 31 August 2017 04:48
To: [hidden email]
Subject: Apply multiplication to all elements in an array

Hi there,

I'm new to coding and can someone guide or direct me to how I can multiply a constant (say 5) to an array of elements:

For e.g. a = newArray(0,1,2,3,4);

I wish to get another array whereby: b = 0, 5, 10, 15, 20

Thanks.
WJ



--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Apply multiplication to all elements in an array

CARL Philippe (LBP)
Dear Kees and WJ,
I'm sorry to disturb, but I would rather do it this way:
a = newArray(0,1,2,3,4);
for (i=0; i<a.length; i++)
        a[i]*=5;
Array.print(a);
My best regards,
Philippe

-----Message d'origine-----
De : ImageJ Interest Group [mailto:[hidden email]] De la part de
Straatman, Kees (Dr.)
Envoyé : jeudi 31 août 2017 09:33
À : [hidden email]
Objet : Re: Apply multiplication to all elements in an array

Dear WJ,

Try

a = newArray(0,1,2,3,4);
b = newArray(5);
for (i=0; i<a.length; i++)
        b[i] = a[i]*5;
Array.print(b);

Best wishes

Kees


-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of
wei.jian
Sent: 31 August 2017 04:48
To: [hidden email]
Subject: Apply multiplication to all elements in an array

Hi there,

I'm new to coding and can someone guide or direct me to how I can multiply a
constant (say 5) to an array of elements:

For e.g. a = newArray(0,1,2,3,4);

I wish to get another array whereby: b = 0, 5, 10, 15, 20

Thanks.
WJ



--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Apply multiplication to all elements in an array

wei.jian
In reply to this post by Krs5
Hi Kees,

The code works but I don't get why you need to have b = newArray(5)?

Do you mind elaborating more on that?

Thanks.
Adam



--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Apply multiplication to all elements in an array

wei.jian
In reply to this post by CARL Philippe (LBP)
Hi Philippe,

Thanks for your reply. Do you mind explaining what *= does?

WJ



--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Apply multiplication to all elements in an array

Nico Stuurman
> Do you mind explaining what *= does?

In most programming languages,

  a *= b;

is the same thing as

a = a * b;

but is shorter, and to some of us, looks better.


Best,

Nico

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Apply multiplication to all elements in an array

wei.jian
Hey Nico,

Thanks for the clarification.

WJ.



--
Sent from: http://imagej.1557.x6.nabble.com/

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html