Adding Arrays...
-
- Lounger
- Posts: 47
- Joined: 26 Jan 2010, 01:05
Adding Arrays...
Good afternoon :D
I'm trying to add two arrays to create a third; all three are 3x3 arrays. Anyway, I get as far as
Dim Matrix1(2,2) As Double
Dim Matrix2(2,2) As Double
Dim Matrix3(2,2) As Double
And then kind of get confused. I want to add Matrix 1 + Matrix 2, and I know I need to step through each location incrementally in Matrix 1 and Matrix 2 (at the same time). I don't want the entire answer, just a healthy shove in the right direction.
I'm trying to add two arrays to create a third; all three are 3x3 arrays. Anyway, I get as far as
Dim Matrix1(2,2) As Double
Dim Matrix2(2,2) As Double
Dim Matrix3(2,2) As Double
And then kind of get confused. I want to add Matrix 1 + Matrix 2, and I know I need to step through each location incrementally in Matrix 1 and Matrix 2 (at the same time). I don't want the entire answer, just a healthy shove in the right direction.
-
- 5StarLounger
- Posts: 818
- Joined: 24 Jan 2010, 15:56
-
- Lounger
- Posts: 47
- Joined: 26 Jan 2010, 01:05
Re: Adding Arrays...
Yep, I understand that, but have no clue how to do it yet
-
- 5StarLounger
- Posts: 818
- Joined: 24 Jan 2010, 15:56
Re: Adding Arrays...
Hard to say much more with it still just being a hint, but:
Myarray3(x, y) = myarray1(x, y) + myarray2(x, y)
Myarray3(x, y) = myarray1(x, y) + myarray2(x, y)
Regards,
Rory
Rory
-
- Lounger
- Posts: 47
- Joined: 26 Jan 2010, 01:05
Re: Adding Arrays...
Code: Select all
For intCount1=0 to 2
For intCount2=0 to 2
Matrix3(intCount1,intCount2)=Matrix1(intCount1,intCount2)+Matrix2(intCount1,intCount2)
Next intCount2
Next intCount1
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Adding Arrays...
Hello. I wanted to ask a very similar question.NYIntensity wrote:Code: Select all
For intCount1=0 to 2 For intCount2=0 to 2 Matrix3(intCount1,intCount2)=Matrix1(intCount1,intCount2)+Matrix2(intCount1,intCount2) Next intCount2 Next intCount1
Is it not possible to add two arrays without looping? Can I multiply each element of an array by a number
without looping?
I'm also trying to add a number to an Excel range, but the following yields an error:
Range("A1:A10") = Range("A1:A10") + 5
Thanks for any advice,
Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
-
- Lounger
- Posts: 47
- Joined: 26 Jan 2010, 01:05
Re: Adding Arrays...
I don't think so; your problem would be kind of easy though:
For intCount1=0 to 2
For intCount2=0 to 2
Range(intCount1,intCount2)=Range(intCount1,intCount2)+5
Next intCount2
Next intCount1
For intCount1=0 to 2
For intCount2=0 to 2
Range(intCount1,intCount2)=Range(intCount1,intCount2)+5
Next intCount2
Next intCount1
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Adding Arrays...
That's a shame.
Why is the instruction
Range("A1:A4") = Range("A1:A4") + 5
a Type Mismatch?
Ta, Andy.
Why is the instruction
Range("A1:A4") = Range("A1:A4") + 5
a Type Mismatch?
Ta, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
-
- Administrator
- Posts: 12631
- Joined: 16 Jan 2010, 15:49
- Location: London, Europe
Re: Adding Arrays...
This is a type mismatch because you are trying to add together a number and a range. This doesn't make any sense to VBA.
StuartR
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Adding Arrays...
Thank you. Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
-
- 5StarLounger
- Posts: 818
- Joined: 24 Jan 2010, 15:56
Re: Adding Arrays...
Ranges can be done easily enough, arrays not so:
Code: Select all
Range("A1:A10").Value = [A1:A10 + 5]
Regards,
Rory
Rory
-
- SilverLounger
- Posts: 2403
- Joined: 05 Feb 2010, 22:21
- Location: London ENGLAND
Re: Adding Arrays...
Wow! I've just picked up on your answer.
I've used [A1:A10] many times, but where did [A1:A10+5] come from? Why/how does this work?
(How did you discover this?) Thanks, Andy.
I've used [A1:A10] many times, but where did [A1:A10+5] come from? Why/how does this work?
(How did you discover this?) Thanks, Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
-
- 5StarLounger
- Posts: 818
- Joined: 24 Jan 2010, 15:56
Re: Adding Arrays...
That notation is a shortcut for Evaluate. It's very useful for manipulating ranges without loops (and sometimes arrays). I can't recall where or when I first came across it, as with most things I know!
Regards,
Rory
Rory