How to add string array into chartcontrol series - c#

I have these arrays:
string[] Line1= data[3].ToString().Split(' ');
string[] Line2= data[4].ToString().Split(' ');
The string array contain only integer values. Data are like -20 -30 -12 0 10 20 30 and so on.
Now want to add these values which is in lineNeg1 to Devexpress Chart Control Series without loop.
As right now things are working but due to loop, system gets too slow. Code sample is here under:
for (int i = 0; i < Line1.Length; i++)
{
int y = int.Parse(Line1[i]);
SeriesPoint pt = new SeriesPoint(i, y);
chartControl1.Series[0].Points.Add(pt);
}
Is there any way that i can do something like: Add string array to series without using loop
maybe like: series[0].addrange[Line1] <- Maybe this kind of something option is available
I know the state is wrong, still just want to give an idea of what i am looking for.

You could use Linq:
int[] ints = Line1.Select(x => int.Parse(x)).ToArray();
It's still a for-loop, but now it's hidden! The compiler needs to convert the strings into ints one by one as they're fundamentally different things and stored quite differently. Strings are objects whereas integers are native types. It's not like Javascript or PHP where strings and integers get converted on the fly unfortunately. So this doesn't help you much, it's just semantic sugar.
Now, as far as adding the series goes, maybe the problem is that the chart redraws every time a point is added. Have you tried your code like this:
chartControl1.SuspendLayout();
for (int i = 0; i < Line1.Length; i++)
{
int y = int.Parse(Line1[i]);
SeriesPoint pt = new SeriesPoint(i, y);
chartControl1.Series[0].Points.Add(pt);
}
chartControl1.ResumeLayout();

Related

from multiple-dimension array double, to int

As the title says, I would like to know if there is a simple way to convert a multiple dimension array of double numbers to the same array, but in int numbers.
Of course we could have two(or more) for loops going to each number and convert them, but I was wondering if there is a simple method to do it? :)
(By the way I am truly sorry if this question has already been asked a lot, but I didn't find any answer!)
Edit: As I lack a lot of informations:
I have for example
double[,] tab1 = {{3.42,1.6523,42.42142},{42.124,932.241, 9.421}};
int[,] tab2;
And I would like to have at the end
tab2 = {{3,1,42}{42,932,9}}
Right now the code I have to do this is
for(int i=0; i<tab1.GetLength(0); i++){
for (int j=0; j<tab1.GetLength(1); j++) {
tab2[i,j] = (int)tab1[i,j];
}
}
Well considering it's a two dimensional array, you can do it using a single for loop and using Array.ConvertAll() method. See an example below. hope gives a pointer
int[] convertedArray = Array.ConvertAll(myDoubleArray, x => (int)x);

passing a result(double type variable) from a method as an element of an array

Background -
I am new to programming in C# and am trying to code a custom indicator. On a trading platform that uses C#. A histogram with
positive Y axis = reqAskArraySum
negative Y axis = reqBidArraySum
The inbuilt compiler shows no errors. However the desired results do not seem to show up.
I know/it is possible there are some platform specific initialization problems i.e. code that I have not entered yet correctly.
Question -
The question here is with regards to the code posted below , which is a part of the whole code.
I would like to know whether the posted code satisfies the below objectives.
The objective here is to get a 'number' using a method.
Then only accept selected 'numbers' into an array. The selection/filtration is done by an 'If' statement
Any help and pointers would be highly appreciated.
int i=0;
double iAskSize = GetLevel2Size(ASK,0); //GetLevel2Size(ASK,0) is the method that helps me to retrieve the ASK side 'numbers'//
double iBidSize = GetLevel2Size(BID,0); //GetLevel2Size(BID,0) is the method that helps me to retrieve the BID side 'numbers' //
if(iAskSize>=AskTH_value) //the number should be >= user defined AskTH_value, this is the filtration of the Ask Side 'numbers'//
I am trying to get the filtered iAskSize 'numbers' into the array
reqAskSize, I believe there is a problem here. However I am not sure
{
double[] reqAskSize = new double[1000];
double reqAskArraySum = 0;
for(i=0;i<reqAskSize.Length;i++)
{
reqAskArraySum = reqAskArraySum + reqAskSize[i];
}
SetValue(0,reqAskArraySum);
}
if(iBidSize>=BidTH_value) **//the number should be >= user defined BidTH_value,this is the filtration of the Bid Side 'numbers'//**
I am trying to get the filtered iBidSize 'numbers' into the array
reqBidSize, I believe there is a problem here. However I am not sure
{
double[] reqBidSize = new double[1000];
double reqBidArraySum = 0;
for(i=0;i<reqBidSize.Length;i++)
{
reqBidArraySum = reqBidArraySum + reqBidSize[i];
}
SetValue(1,-reqBidArraySum);
}
If you are trying to add selected numbers into an array, once a number has been selected (through a conditional like if/else), the number should be set in the array.
For example
int[] array = new int[6];
for(int val = 0; val < 6; val++)
{
if(array[val] > 0)
array[val] = 6;
}
Also to obtain a numeral, a return statement makes clear if anything is got.
You should try and see where a return statement may belong within your code.

ILNumerics using subarrays and matfiles with structs

First question: Can ILNumerics read matfiles with struct? I couldnt make it work.
I then split the file in matlab and I would like to use it for calculations. but i have problems with the subarray function. I would like to do this:
using (ILMatFile matRead = new ILMatFile(#"C:\Temp\Dates.mat"))
{
ILArray<double> Dates = matRead.GetArray<double>("Dates");
double x = 736055-1;
double y = 736237+1;
ILArray<ILLogical> LogDates = (Dates > x && Dates < y);
}
using (ILMatFile matRead = new ILMatFile(#"C:\Temp\Power.mat"))
{
ILArray<double> power = matRead.GetArray<double>("Power");
ILArray<double> tpower = power[LogDates, full];
double avgpower = tpower.Average();
Console.WriteLine(avgpower.ToString());
Console.ReadKey();
}
This doesnt work for a number of reasons. The logical doesnt take my syntax and I dont really get why. But also the subarry in the second block doesnt work, it doesnt know the full keyword (even though the documentation says it shouldand also it doesnt like the logical. obviously I want to average tpower over all columns and only those rows where the logical condition is one.
thanks.
nik
ILLogical is an array itself. You use it like that:
ILLogical LogDates = ILMath.and(Dates > x, Dates < y);
If you still experiencing problems with the subarray, try:
ILArray<double> tpower = power[ILMath.find(LogDates), ILMath.full];
Only, if your class is derived from ILMath, you can ommit the ILMath. identifier! Otherwise, string subarray definitions are sometimes shorter:
ILArray<double> tpower = power[ILMath.find(LogDates), ":"]
In order to take the average over selected rows, reducing to one:
double avgpower = tpower.Average(); // Linq version
double avgpower = (double)ILMath.sumall(tpower) / tpower.S.NumberOfElements; // prob. faster on large data

How can I make a 2D array from this one-line, comma delimited file?

I am trying to figure out how to turn the following, one-line CSV file into a 30x30 2D array.
http://pastebin.com/8NP7s7N0
I've tried looking it up myself, but I just can't seem to wrap my brain around the concept of multidimensional arrays, and I don't know how to turn a one-line file like this into an array of a specified size.
I want to be able to make an array that would look like this when printed:
0,0 = 2
0,1 = 2
All the way to 30,30.
Most of the numbers in the CSV are indeed 2's, but some are 1s. The difference is very important though. I am trying to make collision detection for a game, and this CSV file is the map. All I need left is how to create this array - leave the rest to me. :)
Thank you very much to all, have a nice day.
This should be a complete example using a 5 x 5 grid. I've tried it and seems to work as expected:
namespace ConsoleApplication1
{
using System;
class Program
{
const int MapRows = 5;
const int MapColumns = 5;
static void Main(string[] args)
{
// Create map and the raw data (from file)
var map = new int[MapRows, MapColumns];
string rawMapData = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25";
string[] splitData = rawMapData.Split(',');
int index = 0;
// Loop through data
for (int row = 0; row < MapRows; row++)
{
for (int column = 0; column < MapColumns; column++)
{
// Store in map and show some debug
map[row, column] = int.Parse(splitData[index++]);
Console.WriteLine(string.Format("{0},{1} = {2}", row, column, map[row, column]));
}
}
// Wait for user to read
Console.ReadKey();
}
}
}
Assuming your file is 900 elements first you need to read it in..
something along the lines of
line = myStreamReader.readLine().Split(',').. then in John U's example, value would be the next index in this array called line
I'll let you work out whats missing from my example :P
well, first you need to get the numbers...
var numbers = Read_File_As_String().Split(new char[',']).Select(n => int.Parse(n)).ToList();
then, you need to build your array
const int ROWS = 30;
const int COLS = 30;
var result = new int[ROWS, COLS];
for (int row = 0; row < ROWS; row++)
for (int col = 0; col < COLS; col++)
result[row, col] = numbers[(row * COLS) + col];
for(row=0;row<30;row++)
{
for(col=0;col<30;col++)
{
array[row][col] = value;
}
}
Value would need to be moved along to point to the next thing each time, but I'm sure you can figure that out.
Edited to add: If it's a map it might be easier to store it as an array in the first place.
Since you asked about the concept of multi-dimensional arrays, here are some useful ways of thinking about arrays. Please note these are analogies, meant to help you visualize them.
Think of a 1D array as a list of items (not in the programming sense of list!).
Think of a 2D array as a table (again, not in the programming sense!). In a table (like a spreadsheet) you have rows and columns, and each dimension in your array accesses one of these.
For higher dimensional arrays, it may help to think geometrically. For instance, you can think of 3D arrays as 3-dimensional points in space, and 4D arrays as 4-dimensional points in space-time.
So if you have a single CSV file, start off by conceptualizing how this would be re-structured as a table. Once you have that, you have a pretty straight-forward mapping to the array.

Reversing a integer value list

I have a list of integer values which can be anywhere between 1 and 4.
So, let's say {1,2,4,1,3,2,1,4,4} for instance.
I now want to reverse the values in the following way:
All entries with ...
1 should be converted to 4,
2 should be converted to 3,
3 should be converted to 2,
4 should be converted to 1.
There are numerous ways to do this but I want to take the most efficient approach.
Any thoughts?
for(int i = 0; i < array.Length; i++)
{
array[i] = 5 - array[i];
}
Implement this function:
f(x) = 5 - x
The most efficient will be a for loop with a case statement but it won't be the most flexible or pretty to look at. Any solution you can come up this that only iterates the loop one time could be considered decent solutions since they will all be O(N) performing.
Try the following:
var result = list.Select(item => 5 - item);
I don't know about efficiency, but I'm thinking that first filtering out all duplicates (think there's a LINQ-extension-method for that), then sorting from smallest to biggest, and last creating a hash-map (Dictionary<int, int>) that holds the conversions. Then you can run trough the array like this:
for(int i = 0, l = sortedUniqueArray.Count; i < l; i++) {
dict[sortedUniqueArray[i]] = sortedUniqueArray[l - i];
}
Or something like that. Then you can do the final replacement like this:
orgArray.Select(itm => dict[itm]);
I think good way to write rules of convert and using this rules perform converting.

Categories