+ 1
Is there a better way to print a section of a matrix?
Apparently, I can specify the start/end of row & column to control which portion of the matrix I want to print, but is it the only way to do it in C#? Is there another way like slicing an array with [start:end] like we do in Python? https://sololearn.com/compiler-playground/ce3Kukv7MjjV/?ref=app
12 Answers
+ 3
Bob_Li
Thanks for the examples.
However, I'm working with an established matrix, not a 1d array.
I'm starting to think why C# (so as Java) runs faster than Python.
Put the interpretation thing aside, it looks like, "for loop" for example, C# has a production line with 3 already standing labors, initializer, stop controller and number manipulator (verbose code). And Python only as a range "manager", when the job comes he hires the labors he needs, thus taking some time (less verbose code).
+ 3
Race condition â
+ 2
Wong Hei Ming depending on the focus of what you are trying to achieve, you could get the same result by using Substring() without using the intermediate arr[] array. Lines 10 - 43 could be replaced by this:
// print the entire matrix
int n = 5;
int m = 4;
for (int i = 0; i<n; ++i)
Console.WriteLine(up.Substring(i*m, m));
Console.WriteLine();
//print EFG
int desire_row = 1;
int start = 0;
int end = 3; Console.WriteLine(up.Substring(desire_row*m + start, end - start));
Console.WriteLine();
+ 2
Bob_Li this happens so frequently, when Sololearn delays showing first responses so multiple users respond with similar answers, there ought to be a name for it.
I usually even refresh right before posting and still see no other answer. But after posting I see there were answers many minutes before!
+ 2
Brian
idea collision..đ
On the topic of not creating new arrays, Numpy's views is another good approach. Just create a matrix representation of the same underlying 1d data. It is more efficient than creating new 2d arrays.
Btw, there is such a thing as NumSharp..I have not tried it and it's probably not avaiable in SL.
+ 2
Brian, line 12-18 is used to create the matrix, and line 21-28 just prints the matrix so I can see it works correctly. We can comment-out line 21-28 and pretend there is an unknown size of 2d matrix.
In this example, since we already know the matrix size ahead, try...catch is not used in here.
To think in Python way, if it is a 2d list and I want to print EFG, or more precisely, the first 3 elements from row 1, I would simply write print(arr[1][:3]), that is the reason why line 33 exists.
My concern is that do we necessarily need to manually declare the "desire_row", "start" and "end" variables to archive the same result as we do in Python.
+ 2
Wong Hei Ming
The choice of using a 2d array complicates things if you want to pick and print entire rows.
Use a jagged array instead. You will have greater flexibility.
I also discovered Index and Range types.
Index:
[^i ] is reverse or negative indexing. starts at the array end.
Range:
[i1..i2] is like Python's [i1 : i2]]. Notice 2 dots .. is used, so like a horizontal : đ
.
https://sololearn.com/compiler-playground/cZo1npp3IQzB/?ref=app
+ 2
It is a shame arr2[1][0..3] = "X"; (assign character X to column 0 to 2 in row 1) doesn't work.
In the newer version of my greek pattern it draws continues lines only, thus arr[row][start:end] = "X" will come in handy if available.
Guess another for loop is needed to get the job done.
+ 1
compiled languages always have the speed advantage. If the compiler sees it fit, entire sections of your code might just be abstracted away completely.
Brian
yes, my thought exactly. Just display it as a matrix without actually creating one.
+ 1
Bob_Li
I'm asking because I want to port the greek pattern code into C#.
Instead of accessing the elements, I fill in the elements, and they share the same concept.
Only I need to implement my own np.rot90() in C# (also part of the practice).
+ 1
Wong Hei Ming
yeah, it's not as powerful as Python 's slicing syntax. You can read it but cannot assign to it.
C# have ArraySegment. You can modify the base array through it. But creating and using it is not as straight forward as Python slicing.