matrix in visual basicWriting a matrix to a text fileAn optimal way of finding the first row with only '0' elements in matrix?Matrix input result (Python)Impossible to remove rows from matrix with Math.NET NumericsIn-Place CUDA Kernel for Rectangular Matrix TransposeConstrained maximization of the sum of square submatricescreate a 24*30 matrix from a 100*30 matrix excel vbaSuppose we have a matrix, 100 rows and 100 columns, of 2-byte integersMatrix class C++Java OOP Changing the position of 2d matrix?How to scan integers into a square matrix (2D array)? (Matrix size unknown before compilation)
Car headlights in a world without electricity
How can a day be of 24 hours?
Am I breaking OOP practice with this architecture?
What do you call someone who asks many questions?
Finitely generated matrix groups whose eigenvalues are all algebraic
Does Dispel Magic work on Tiny Hut?
Is there an online compendium of Rav Moshe teshuvos in English that exists?
Why were 5.25" floppy drives cheaper than 8"?
How to show a landlord what we have in savings?
One verb to replace 'be a member of' a club
What is a Samsaran Word™?
Do creatures with a listed speed of "0 ft., fly 30 ft. (hover)" ever touch the ground?
how do we prove that a sum of two periods is still a period?
Why didn't Boeing produce its own regional jet?
What is the opposite of "eschatology"?
Venezuelan girlfriend wants to travel the USA to be with me. What is the process?
In Bayesian inference, why are some terms dropped from the posterior predictive?
How to install cross-compiler on Ubuntu 18.04?
Do Iron Man suits sport waste management systems?
Should I tell management that I intend to leave due to bad software development practices?
How to compactly explain secondary and tertiary characters without resorting to stereotypes?
What is the fastest integer factorization to break RSA?
How obscure is the use of 令 in 令和?
files created then deleted at every second in tmp directory
matrix in visual basic
Writing a matrix to a text fileAn optimal way of finding the first row with only '0' elements in matrix?Matrix input result (Python)Impossible to remove rows from matrix with Math.NET NumericsIn-Place CUDA Kernel for Rectangular Matrix TransposeConstrained maximization of the sum of square submatricescreate a 24*30 matrix from a 100*30 matrix excel vbaSuppose we have a matrix, 100 rows and 100 columns, of 2-byte integersMatrix class C++Java OOP Changing the position of 2d matrix?How to scan integers into a square matrix (2D array)? (Matrix size unknown before compilation)
so i have a task to create a square matrix then from that return the average of the rows and columns, having some problem though
Dim m, n, i, j As Integer
Dim A(2, 2) As Integer
Console.Write(vbLf & "Enter The Matrix Elements any two : ")
For i = 0 To 2 - 1
For j = 0 To 2 = 1
A(i, j) = Convert.ToInt16(Console.ReadLine())
Next
Next
Console.Clear()
Console.WriteLine(vbLf & "Matrix A : ")
For i = 0 To 2 - 1
For j = 0 To 2 - 1
Console.Write(vbLf & "0", A(i, j))
Next
Console.WriteLine(" ")
Next
Console.WriteLine(vbLf & "Transpose Matrix : ")
For i = 0 To 2 - 1
For j = 0 To 2 - 1
Console.Write(vbLf & "0", A(j, i))
Next
Console.WriteLine(" ")
Next
End Sub
This is my code, i know its supposed to be a square matrix so how would i do that, when i also try inputing the values it does not let me go past the next one, does anyone have code for a square matrix that gets its values from a user, or someone can help me please
arrays vb.net matrix
add a comment |
so i have a task to create a square matrix then from that return the average of the rows and columns, having some problem though
Dim m, n, i, j As Integer
Dim A(2, 2) As Integer
Console.Write(vbLf & "Enter The Matrix Elements any two : ")
For i = 0 To 2 - 1
For j = 0 To 2 = 1
A(i, j) = Convert.ToInt16(Console.ReadLine())
Next
Next
Console.Clear()
Console.WriteLine(vbLf & "Matrix A : ")
For i = 0 To 2 - 1
For j = 0 To 2 - 1
Console.Write(vbLf & "0", A(i, j))
Next
Console.WriteLine(" ")
Next
Console.WriteLine(vbLf & "Transpose Matrix : ")
For i = 0 To 2 - 1
For j = 0 To 2 - 1
Console.Write(vbLf & "0", A(j, i))
Next
Console.WriteLine(" ")
Next
End Sub
This is my code, i know its supposed to be a square matrix so how would i do that, when i also try inputing the values it does not let me go past the next one, does anyone have code for a square matrix that gets its values from a user, or someone can help me please
arrays vb.net matrix
1
Is For j = 0 To 2 = 1 a typo?
– Honeyboy Wilson
Mar 21 at 20:45
How big do you want this matrix? In vb, the subscript is the index of the last item, soDim A(2,2) As Integer
is actually a 3x3 array.
– Joel Coehoorn
Mar 21 at 20:48
Also, I see this:For j = 0 To 2 = 1
, which might just be a typo moving code to Stack Overflow, but is certainly not what you intend with the second=
sign.
– Joel Coehoorn
Mar 21 at 20:50
@JoelCoehoorn i gave this code to someone and they edited that in, i don't know why, basically this is what i want, if say its a 2x2 matix, user is requested to enter, the values for the 4 values in the array, then the array is displayed, the values being (a, b) (c, d) - from this i can do the averages and other things, but its supposed to be a 2d array so really confused, appreciate ur help aswell
– loco3424
Mar 21 at 21:12
add a comment |
so i have a task to create a square matrix then from that return the average of the rows and columns, having some problem though
Dim m, n, i, j As Integer
Dim A(2, 2) As Integer
Console.Write(vbLf & "Enter The Matrix Elements any two : ")
For i = 0 To 2 - 1
For j = 0 To 2 = 1
A(i, j) = Convert.ToInt16(Console.ReadLine())
Next
Next
Console.Clear()
Console.WriteLine(vbLf & "Matrix A : ")
For i = 0 To 2 - 1
For j = 0 To 2 - 1
Console.Write(vbLf & "0", A(i, j))
Next
Console.WriteLine(" ")
Next
Console.WriteLine(vbLf & "Transpose Matrix : ")
For i = 0 To 2 - 1
For j = 0 To 2 - 1
Console.Write(vbLf & "0", A(j, i))
Next
Console.WriteLine(" ")
Next
End Sub
This is my code, i know its supposed to be a square matrix so how would i do that, when i also try inputing the values it does not let me go past the next one, does anyone have code for a square matrix that gets its values from a user, or someone can help me please
arrays vb.net matrix
so i have a task to create a square matrix then from that return the average of the rows and columns, having some problem though
Dim m, n, i, j As Integer
Dim A(2, 2) As Integer
Console.Write(vbLf & "Enter The Matrix Elements any two : ")
For i = 0 To 2 - 1
For j = 0 To 2 = 1
A(i, j) = Convert.ToInt16(Console.ReadLine())
Next
Next
Console.Clear()
Console.WriteLine(vbLf & "Matrix A : ")
For i = 0 To 2 - 1
For j = 0 To 2 - 1
Console.Write(vbLf & "0", A(i, j))
Next
Console.WriteLine(" ")
Next
Console.WriteLine(vbLf & "Transpose Matrix : ")
For i = 0 To 2 - 1
For j = 0 To 2 - 1
Console.Write(vbLf & "0", A(j, i))
Next
Console.WriteLine(" ")
Next
End Sub
This is my code, i know its supposed to be a square matrix so how would i do that, when i also try inputing the values it does not let me go past the next one, does anyone have code for a square matrix that gets its values from a user, or someone can help me please
arrays vb.net matrix
arrays vb.net matrix
asked Mar 21 at 20:40
loco3424loco3424
64
64
1
Is For j = 0 To 2 = 1 a typo?
– Honeyboy Wilson
Mar 21 at 20:45
How big do you want this matrix? In vb, the subscript is the index of the last item, soDim A(2,2) As Integer
is actually a 3x3 array.
– Joel Coehoorn
Mar 21 at 20:48
Also, I see this:For j = 0 To 2 = 1
, which might just be a typo moving code to Stack Overflow, but is certainly not what you intend with the second=
sign.
– Joel Coehoorn
Mar 21 at 20:50
@JoelCoehoorn i gave this code to someone and they edited that in, i don't know why, basically this is what i want, if say its a 2x2 matix, user is requested to enter, the values for the 4 values in the array, then the array is displayed, the values being (a, b) (c, d) - from this i can do the averages and other things, but its supposed to be a 2d array so really confused, appreciate ur help aswell
– loco3424
Mar 21 at 21:12
add a comment |
1
Is For j = 0 To 2 = 1 a typo?
– Honeyboy Wilson
Mar 21 at 20:45
How big do you want this matrix? In vb, the subscript is the index of the last item, soDim A(2,2) As Integer
is actually a 3x3 array.
– Joel Coehoorn
Mar 21 at 20:48
Also, I see this:For j = 0 To 2 = 1
, which might just be a typo moving code to Stack Overflow, but is certainly not what you intend with the second=
sign.
– Joel Coehoorn
Mar 21 at 20:50
@JoelCoehoorn i gave this code to someone and they edited that in, i don't know why, basically this is what i want, if say its a 2x2 matix, user is requested to enter, the values for the 4 values in the array, then the array is displayed, the values being (a, b) (c, d) - from this i can do the averages and other things, but its supposed to be a 2d array so really confused, appreciate ur help aswell
– loco3424
Mar 21 at 21:12
1
1
Is For j = 0 To 2 = 1 a typo?
– Honeyboy Wilson
Mar 21 at 20:45
Is For j = 0 To 2 = 1 a typo?
– Honeyboy Wilson
Mar 21 at 20:45
How big do you want this matrix? In vb, the subscript is the index of the last item, so
Dim A(2,2) As Integer
is actually a 3x3 array.– Joel Coehoorn
Mar 21 at 20:48
How big do you want this matrix? In vb, the subscript is the index of the last item, so
Dim A(2,2) As Integer
is actually a 3x3 array.– Joel Coehoorn
Mar 21 at 20:48
Also, I see this:
For j = 0 To 2 = 1
, which might just be a typo moving code to Stack Overflow, but is certainly not what you intend with the second =
sign.– Joel Coehoorn
Mar 21 at 20:50
Also, I see this:
For j = 0 To 2 = 1
, which might just be a typo moving code to Stack Overflow, but is certainly not what you intend with the second =
sign.– Joel Coehoorn
Mar 21 at 20:50
@JoelCoehoorn i gave this code to someone and they edited that in, i don't know why, basically this is what i want, if say its a 2x2 matix, user is requested to enter, the values for the 4 values in the array, then the array is displayed, the values being (a, b) (c, d) - from this i can do the averages and other things, but its supposed to be a 2d array so really confused, appreciate ur help aswell
– loco3424
Mar 21 at 21:12
@JoelCoehoorn i gave this code to someone and they edited that in, i don't know why, basically this is what i want, if say its a 2x2 matix, user is requested to enter, the values for the 4 values in the array, then the array is displayed, the values being (a, b) (c, d) - from this i can do the averages and other things, but its supposed to be a 2d array so really confused, appreciate ur help aswell
– loco3424
Mar 21 at 21:12
add a comment |
1 Answer
1
active
oldest
votes
To make it generic, use the GetUpperBound() function to determine how many elements are in your array for each dimension (it should be the same, however, since you have a square matrix):
Sub Main()
Dim size As Integer = 3
Dim numberWidth As Integer = 2
Dim format As String = "D" & numberWidth
Dim A(size - 1, size - 1) As Integer
For i As Integer = 0 To A.GetUpperBound(0)
For j As Integer = 0 To A.GetUpperBound(1)
Console.Write(String.Format("Enter The Matrix Element at A[Row 0, Col 1]: ", i, j))
A(i, j) = Convert.ToInt16(Console.ReadLine())
Next
Next
Console.WriteLine("Matrix A :")
For i As Integer = 0 To A.GetUpperBound(0)
Console.Write("| ")
For j As Integer = 0 To A.GetUpperBound(1)
Console.Write("0 ", A(i, j).ToString(format))
Next
Console.WriteLine("|")
Next
Console.WriteLine("Transpose Matrix :")
For i As Integer = 0 To A.GetUpperBound(0)
Console.Write("| ")
For j As Integer = 0 To A.GetUpperBound(1)
Console.Write("0 ", A(j, i).ToString(format))
Next
Console.WriteLine("|")
Next
Console.ReadLine()
End Sub
Output:
Enter The Matrix Element at A[Row 0, Col 0]: 1
Enter The Matrix Element at A[Row 0, Col 1]: 2
Enter The Matrix Element at A[Row 0, Col 2]: 3
Enter The Matrix Element at A[Row 1, Col 0]: 4
Enter The Matrix Element at A[Row 1, Col 1]: 5
Enter The Matrix Element at A[Row 1, Col 2]: 6
Enter The Matrix Element at A[Row 2, Col 0]: 7
Enter The Matrix Element at A[Row 2, Col 1]: 8
Enter The Matrix Element at A[Row 2, Col 2]: 9
Matrix A :
| 01 02 03 |
| 04 05 06 |
| 07 08 09 |
Transpose Matrix :
| 01 04 07 |
| 02 05 08 |
| 03 06 09 |
Here's a quick example of how to compute the SUM of each ROW:
Dim Total as Integer
For row As Integer = 0 To A.GetUpperBound(0)
Total = 0 ' reset for each row/column
For col As Integer = 0 To A.GetUpperBound(1)
Total = Total + A(row, col)
Next
Console.WriteLine("Row " & row & " total: " & Total)
' ... do something else with "Total" here; like compute an average ...
Next
The code for computing the total of columns will be very similar, just swap the positions of the For row
and For col
lines.
thats cool bro yeah learnt about upper and lower bound in my last post, thanks for explaining, however how would i use this to be able to calculate the rows and column sum,
– loco3424
Mar 21 at 21:30
You can use the same nested for loop structure already being demonstrated for the output, to compute the sum of each row. You'd switch the order of the rows/cols for loops to get the column sums. I'll post something in a bit, have to hit the road.
– Idle_Mind
Mar 21 at 21:52
Cool, thanks, much appericated, have good one on the roads
– loco3424
Mar 21 at 22:04
yes i would have to calculate the averages and wanted to try this, however one problem, when i tried to enter this code just to see if it works, it would not work just got a error
– loco3424
Mar 22 at 15:55
1
smashed it mate, did the averages today and the column count today, thanks for explaining also bro
– loco3424
Mar 23 at 15:57
|
show 12 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55288915%2fmatrix-in-visual-basic%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To make it generic, use the GetUpperBound() function to determine how many elements are in your array for each dimension (it should be the same, however, since you have a square matrix):
Sub Main()
Dim size As Integer = 3
Dim numberWidth As Integer = 2
Dim format As String = "D" & numberWidth
Dim A(size - 1, size - 1) As Integer
For i As Integer = 0 To A.GetUpperBound(0)
For j As Integer = 0 To A.GetUpperBound(1)
Console.Write(String.Format("Enter The Matrix Element at A[Row 0, Col 1]: ", i, j))
A(i, j) = Convert.ToInt16(Console.ReadLine())
Next
Next
Console.WriteLine("Matrix A :")
For i As Integer = 0 To A.GetUpperBound(0)
Console.Write("| ")
For j As Integer = 0 To A.GetUpperBound(1)
Console.Write("0 ", A(i, j).ToString(format))
Next
Console.WriteLine("|")
Next
Console.WriteLine("Transpose Matrix :")
For i As Integer = 0 To A.GetUpperBound(0)
Console.Write("| ")
For j As Integer = 0 To A.GetUpperBound(1)
Console.Write("0 ", A(j, i).ToString(format))
Next
Console.WriteLine("|")
Next
Console.ReadLine()
End Sub
Output:
Enter The Matrix Element at A[Row 0, Col 0]: 1
Enter The Matrix Element at A[Row 0, Col 1]: 2
Enter The Matrix Element at A[Row 0, Col 2]: 3
Enter The Matrix Element at A[Row 1, Col 0]: 4
Enter The Matrix Element at A[Row 1, Col 1]: 5
Enter The Matrix Element at A[Row 1, Col 2]: 6
Enter The Matrix Element at A[Row 2, Col 0]: 7
Enter The Matrix Element at A[Row 2, Col 1]: 8
Enter The Matrix Element at A[Row 2, Col 2]: 9
Matrix A :
| 01 02 03 |
| 04 05 06 |
| 07 08 09 |
Transpose Matrix :
| 01 04 07 |
| 02 05 08 |
| 03 06 09 |
Here's a quick example of how to compute the SUM of each ROW:
Dim Total as Integer
For row As Integer = 0 To A.GetUpperBound(0)
Total = 0 ' reset for each row/column
For col As Integer = 0 To A.GetUpperBound(1)
Total = Total + A(row, col)
Next
Console.WriteLine("Row " & row & " total: " & Total)
' ... do something else with "Total" here; like compute an average ...
Next
The code for computing the total of columns will be very similar, just swap the positions of the For row
and For col
lines.
thats cool bro yeah learnt about upper and lower bound in my last post, thanks for explaining, however how would i use this to be able to calculate the rows and column sum,
– loco3424
Mar 21 at 21:30
You can use the same nested for loop structure already being demonstrated for the output, to compute the sum of each row. You'd switch the order of the rows/cols for loops to get the column sums. I'll post something in a bit, have to hit the road.
– Idle_Mind
Mar 21 at 21:52
Cool, thanks, much appericated, have good one on the roads
– loco3424
Mar 21 at 22:04
yes i would have to calculate the averages and wanted to try this, however one problem, when i tried to enter this code just to see if it works, it would not work just got a error
– loco3424
Mar 22 at 15:55
1
smashed it mate, did the averages today and the column count today, thanks for explaining also bro
– loco3424
Mar 23 at 15:57
|
show 12 more comments
To make it generic, use the GetUpperBound() function to determine how many elements are in your array for each dimension (it should be the same, however, since you have a square matrix):
Sub Main()
Dim size As Integer = 3
Dim numberWidth As Integer = 2
Dim format As String = "D" & numberWidth
Dim A(size - 1, size - 1) As Integer
For i As Integer = 0 To A.GetUpperBound(0)
For j As Integer = 0 To A.GetUpperBound(1)
Console.Write(String.Format("Enter The Matrix Element at A[Row 0, Col 1]: ", i, j))
A(i, j) = Convert.ToInt16(Console.ReadLine())
Next
Next
Console.WriteLine("Matrix A :")
For i As Integer = 0 To A.GetUpperBound(0)
Console.Write("| ")
For j As Integer = 0 To A.GetUpperBound(1)
Console.Write("0 ", A(i, j).ToString(format))
Next
Console.WriteLine("|")
Next
Console.WriteLine("Transpose Matrix :")
For i As Integer = 0 To A.GetUpperBound(0)
Console.Write("| ")
For j As Integer = 0 To A.GetUpperBound(1)
Console.Write("0 ", A(j, i).ToString(format))
Next
Console.WriteLine("|")
Next
Console.ReadLine()
End Sub
Output:
Enter The Matrix Element at A[Row 0, Col 0]: 1
Enter The Matrix Element at A[Row 0, Col 1]: 2
Enter The Matrix Element at A[Row 0, Col 2]: 3
Enter The Matrix Element at A[Row 1, Col 0]: 4
Enter The Matrix Element at A[Row 1, Col 1]: 5
Enter The Matrix Element at A[Row 1, Col 2]: 6
Enter The Matrix Element at A[Row 2, Col 0]: 7
Enter The Matrix Element at A[Row 2, Col 1]: 8
Enter The Matrix Element at A[Row 2, Col 2]: 9
Matrix A :
| 01 02 03 |
| 04 05 06 |
| 07 08 09 |
Transpose Matrix :
| 01 04 07 |
| 02 05 08 |
| 03 06 09 |
Here's a quick example of how to compute the SUM of each ROW:
Dim Total as Integer
For row As Integer = 0 To A.GetUpperBound(0)
Total = 0 ' reset for each row/column
For col As Integer = 0 To A.GetUpperBound(1)
Total = Total + A(row, col)
Next
Console.WriteLine("Row " & row & " total: " & Total)
' ... do something else with "Total" here; like compute an average ...
Next
The code for computing the total of columns will be very similar, just swap the positions of the For row
and For col
lines.
thats cool bro yeah learnt about upper and lower bound in my last post, thanks for explaining, however how would i use this to be able to calculate the rows and column sum,
– loco3424
Mar 21 at 21:30
You can use the same nested for loop structure already being demonstrated for the output, to compute the sum of each row. You'd switch the order of the rows/cols for loops to get the column sums. I'll post something in a bit, have to hit the road.
– Idle_Mind
Mar 21 at 21:52
Cool, thanks, much appericated, have good one on the roads
– loco3424
Mar 21 at 22:04
yes i would have to calculate the averages and wanted to try this, however one problem, when i tried to enter this code just to see if it works, it would not work just got a error
– loco3424
Mar 22 at 15:55
1
smashed it mate, did the averages today and the column count today, thanks for explaining also bro
– loco3424
Mar 23 at 15:57
|
show 12 more comments
To make it generic, use the GetUpperBound() function to determine how many elements are in your array for each dimension (it should be the same, however, since you have a square matrix):
Sub Main()
Dim size As Integer = 3
Dim numberWidth As Integer = 2
Dim format As String = "D" & numberWidth
Dim A(size - 1, size - 1) As Integer
For i As Integer = 0 To A.GetUpperBound(0)
For j As Integer = 0 To A.GetUpperBound(1)
Console.Write(String.Format("Enter The Matrix Element at A[Row 0, Col 1]: ", i, j))
A(i, j) = Convert.ToInt16(Console.ReadLine())
Next
Next
Console.WriteLine("Matrix A :")
For i As Integer = 0 To A.GetUpperBound(0)
Console.Write("| ")
For j As Integer = 0 To A.GetUpperBound(1)
Console.Write("0 ", A(i, j).ToString(format))
Next
Console.WriteLine("|")
Next
Console.WriteLine("Transpose Matrix :")
For i As Integer = 0 To A.GetUpperBound(0)
Console.Write("| ")
For j As Integer = 0 To A.GetUpperBound(1)
Console.Write("0 ", A(j, i).ToString(format))
Next
Console.WriteLine("|")
Next
Console.ReadLine()
End Sub
Output:
Enter The Matrix Element at A[Row 0, Col 0]: 1
Enter The Matrix Element at A[Row 0, Col 1]: 2
Enter The Matrix Element at A[Row 0, Col 2]: 3
Enter The Matrix Element at A[Row 1, Col 0]: 4
Enter The Matrix Element at A[Row 1, Col 1]: 5
Enter The Matrix Element at A[Row 1, Col 2]: 6
Enter The Matrix Element at A[Row 2, Col 0]: 7
Enter The Matrix Element at A[Row 2, Col 1]: 8
Enter The Matrix Element at A[Row 2, Col 2]: 9
Matrix A :
| 01 02 03 |
| 04 05 06 |
| 07 08 09 |
Transpose Matrix :
| 01 04 07 |
| 02 05 08 |
| 03 06 09 |
Here's a quick example of how to compute the SUM of each ROW:
Dim Total as Integer
For row As Integer = 0 To A.GetUpperBound(0)
Total = 0 ' reset for each row/column
For col As Integer = 0 To A.GetUpperBound(1)
Total = Total + A(row, col)
Next
Console.WriteLine("Row " & row & " total: " & Total)
' ... do something else with "Total" here; like compute an average ...
Next
The code for computing the total of columns will be very similar, just swap the positions of the For row
and For col
lines.
To make it generic, use the GetUpperBound() function to determine how many elements are in your array for each dimension (it should be the same, however, since you have a square matrix):
Sub Main()
Dim size As Integer = 3
Dim numberWidth As Integer = 2
Dim format As String = "D" & numberWidth
Dim A(size - 1, size - 1) As Integer
For i As Integer = 0 To A.GetUpperBound(0)
For j As Integer = 0 To A.GetUpperBound(1)
Console.Write(String.Format("Enter The Matrix Element at A[Row 0, Col 1]: ", i, j))
A(i, j) = Convert.ToInt16(Console.ReadLine())
Next
Next
Console.WriteLine("Matrix A :")
For i As Integer = 0 To A.GetUpperBound(0)
Console.Write("| ")
For j As Integer = 0 To A.GetUpperBound(1)
Console.Write("0 ", A(i, j).ToString(format))
Next
Console.WriteLine("|")
Next
Console.WriteLine("Transpose Matrix :")
For i As Integer = 0 To A.GetUpperBound(0)
Console.Write("| ")
For j As Integer = 0 To A.GetUpperBound(1)
Console.Write("0 ", A(j, i).ToString(format))
Next
Console.WriteLine("|")
Next
Console.ReadLine()
End Sub
Output:
Enter The Matrix Element at A[Row 0, Col 0]: 1
Enter The Matrix Element at A[Row 0, Col 1]: 2
Enter The Matrix Element at A[Row 0, Col 2]: 3
Enter The Matrix Element at A[Row 1, Col 0]: 4
Enter The Matrix Element at A[Row 1, Col 1]: 5
Enter The Matrix Element at A[Row 1, Col 2]: 6
Enter The Matrix Element at A[Row 2, Col 0]: 7
Enter The Matrix Element at A[Row 2, Col 1]: 8
Enter The Matrix Element at A[Row 2, Col 2]: 9
Matrix A :
| 01 02 03 |
| 04 05 06 |
| 07 08 09 |
Transpose Matrix :
| 01 04 07 |
| 02 05 08 |
| 03 06 09 |
Here's a quick example of how to compute the SUM of each ROW:
Dim Total as Integer
For row As Integer = 0 To A.GetUpperBound(0)
Total = 0 ' reset for each row/column
For col As Integer = 0 To A.GetUpperBound(1)
Total = Total + A(row, col)
Next
Console.WriteLine("Row " & row & " total: " & Total)
' ... do something else with "Total" here; like compute an average ...
Next
The code for computing the total of columns will be very similar, just swap the positions of the For row
and For col
lines.
edited Mar 22 at 21:26
answered Mar 21 at 21:24
Idle_MindIdle_Mind
24k21524
24k21524
thats cool bro yeah learnt about upper and lower bound in my last post, thanks for explaining, however how would i use this to be able to calculate the rows and column sum,
– loco3424
Mar 21 at 21:30
You can use the same nested for loop structure already being demonstrated for the output, to compute the sum of each row. You'd switch the order of the rows/cols for loops to get the column sums. I'll post something in a bit, have to hit the road.
– Idle_Mind
Mar 21 at 21:52
Cool, thanks, much appericated, have good one on the roads
– loco3424
Mar 21 at 22:04
yes i would have to calculate the averages and wanted to try this, however one problem, when i tried to enter this code just to see if it works, it would not work just got a error
– loco3424
Mar 22 at 15:55
1
smashed it mate, did the averages today and the column count today, thanks for explaining also bro
– loco3424
Mar 23 at 15:57
|
show 12 more comments
thats cool bro yeah learnt about upper and lower bound in my last post, thanks for explaining, however how would i use this to be able to calculate the rows and column sum,
– loco3424
Mar 21 at 21:30
You can use the same nested for loop structure already being demonstrated for the output, to compute the sum of each row. You'd switch the order of the rows/cols for loops to get the column sums. I'll post something in a bit, have to hit the road.
– Idle_Mind
Mar 21 at 21:52
Cool, thanks, much appericated, have good one on the roads
– loco3424
Mar 21 at 22:04
yes i would have to calculate the averages and wanted to try this, however one problem, when i tried to enter this code just to see if it works, it would not work just got a error
– loco3424
Mar 22 at 15:55
1
smashed it mate, did the averages today and the column count today, thanks for explaining also bro
– loco3424
Mar 23 at 15:57
thats cool bro yeah learnt about upper and lower bound in my last post, thanks for explaining, however how would i use this to be able to calculate the rows and column sum,
– loco3424
Mar 21 at 21:30
thats cool bro yeah learnt about upper and lower bound in my last post, thanks for explaining, however how would i use this to be able to calculate the rows and column sum,
– loco3424
Mar 21 at 21:30
You can use the same nested for loop structure already being demonstrated for the output, to compute the sum of each row. You'd switch the order of the rows/cols for loops to get the column sums. I'll post something in a bit, have to hit the road.
– Idle_Mind
Mar 21 at 21:52
You can use the same nested for loop structure already being demonstrated for the output, to compute the sum of each row. You'd switch the order of the rows/cols for loops to get the column sums. I'll post something in a bit, have to hit the road.
– Idle_Mind
Mar 21 at 21:52
Cool, thanks, much appericated, have good one on the roads
– loco3424
Mar 21 at 22:04
Cool, thanks, much appericated, have good one on the roads
– loco3424
Mar 21 at 22:04
yes i would have to calculate the averages and wanted to try this, however one problem, when i tried to enter this code just to see if it works, it would not work just got a error
– loco3424
Mar 22 at 15:55
yes i would have to calculate the averages and wanted to try this, however one problem, when i tried to enter this code just to see if it works, it would not work just got a error
– loco3424
Mar 22 at 15:55
1
1
smashed it mate, did the averages today and the column count today, thanks for explaining also bro
– loco3424
Mar 23 at 15:57
smashed it mate, did the averages today and the column count today, thanks for explaining also bro
– loco3424
Mar 23 at 15:57
|
show 12 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55288915%2fmatrix-in-visual-basic%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Is For j = 0 To 2 = 1 a typo?
– Honeyboy Wilson
Mar 21 at 20:45
How big do you want this matrix? In vb, the subscript is the index of the last item, so
Dim A(2,2) As Integer
is actually a 3x3 array.– Joel Coehoorn
Mar 21 at 20:48
Also, I see this:
For j = 0 To 2 = 1
, which might just be a typo moving code to Stack Overflow, but is certainly not what you intend with the second=
sign.– Joel Coehoorn
Mar 21 at 20:50
@JoelCoehoorn i gave this code to someone and they edited that in, i don't know why, basically this is what i want, if say its a 2x2 matix, user is requested to enter, the values for the 4 values in the array, then the array is displayed, the values being (a, b) (c, d) - from this i can do the averages and other things, but its supposed to be a 2d array so really confused, appreciate ur help aswell
– loco3424
Mar 21 at 21:12