Hello all,
I actually have a big question. I am trying to create some sort of neural pathway type code, but I was trying to make it simple. However, I am not sure how to make a customizable matrix. I first thought of making an array in an array like "double node[][];", but that would only give me a matrix 2 wide, and I need more then just two wide sadly. I want to just give it two variables, height and width, and then initialize the variables I can use for this matrix. I also wanted this in a method, but not quite sure how to declare global variables if it is possible within a method. I was trying to avoid just using a bunch of "double node;" lines to keep modifying fast. Any suggestions or help would be greatly appreciated.
I don't understand the problem. double node[][] doesn't specify the number of elements in either dimension.
double node[3][3] = {0};
defines a 3x3 matrix.
double node[3][3][3] = {0};
defines a 3x3x3 matrix.
Oh, so it does... For some reason, when I tried to think in my head, I thought it would be just two arrays for some reason . I wasn't thinking right. Thank you very much! However, I have ran into the issue of setting the array to declare with variables. It gives me an error saying that:
AIduino.ino: In function 'void createNodes(int, int)':
AIduino.ino:21:35: error: variable-sized object 'nodes' may not be initialized
Error compiling.
all i wrote was :
void createNodes(int height, int column){
float nodes[height][column] = {0};
}
Also do you happen to know how to declare that as a global variable inside of a method?
Do you happen to know how to declare that as a global variable inside of a method?
I don't think that is possible, and I can't imagine why you would want to do that.
A global variable should be right up front, where everyone can see what it is.
I am not sure what you mean by global inside a method, but perhaps you want to use the keyword "static".
It appears that you want to have variable array dimensions, where the dimensions are defined inside a function. Unfortunately, the concept and implementation of variable array dimensions (especially for multidimensional arrays) is IMO a can of worms in C/C++ and very prone to errors.
Avoid doing so if you can find another way to write your program.
What I was trying to do, was depending on the information obtained from the user, it would create an array, and then from that array, it would start processing the information and try and come up with a solution. I was hoping to save memory by just making the arrays as I go, and not have them at the beginning, so that it would only use what it needs. But the array also has to be used by other functions, so I do not think static would work. And I will try and find a way to avoid the variable array, thanks!
by just making the arrays as I go
malloc() is used to allocate memory for such cases, but on an Arduino, you don't have much memory anyway.
Ezrai:
What I was trying to do, was depending on the information obtained from the user, it would create an array, and then from that array, it would start processing the information and try and come up with a solution. I was hoping to save memory by just making the arrays as I go, and not have them at the beginning, so that it would only use what it needs. But the array also has to be used by other functions, so I do not think static would work. And I will try and find a way to avoid the variable array, thanks!
But think about it. Any memory you save and then use for something else you wouldn't have available to make a bigger array. Any memory you have left over that you don't use that is reserved for in case you ever need a bigger array, is wasted anyway. You might as well have had it reserved for the array. So you really just as good to just make the thing as big as you want to begin with. It's not really logical to try to save memory in that way.
Delta_G:
So you really just as good to just make the thing as big as you want to begin with.
Well the issue is that it is just user input, so it doesn't have a limit in theory. I understand that would also go over the arduino's limits, but I don't want to max out its memory on the array as the process could be just as draining. And because of the neural array processing, it could use anything from 2 to 100 or more arrays in the width just because of the complications with processing speech.
Also, I looked into the malloc(), but I think I would end up corrupting more data then I would process properly.
If there is a limit, and there is, at which you are using up your memory and affecting the other processes then you need to enforce that on the user else the user will break your code. Any memory you don't use up to that limit is of no use to you. Saving it doesn't get you anything. So whether you define that array to take up all that space or not you still have to reserve at least that much and put a max limit on it. There is absolutely no advantage to defining the array size from a function In either of those cases. If you need that memory then you cant allow the user to have it. So in your design you'll have to think about the maximum size you want that array and leave room for it anyway. There is no advantage to sizing that array from some function.
You should implement your ideas on a PC before trying to do so on an Arduino. At least that way you are unlikely to encounter mysterious errors due to limited memory.
I like Code::Blocks, which is a free and very convenient development package for PCs. It is really easy to write and debug a console application with it.
In C, the size of the inner array of a two-dimensional array is part of the array's compile-time type.
If you want variable multidimensional size, use a single dimensional array and use it in blocks: foo[row * nCols + col]. YOu can even use a class and overload the C '[' operator, if you want, and there are plenty of 'matrix' classes out there that do exactly this.
Your real problem is that multidimensional arrays use up way more memory than you think. "Oh, I'll just make a twenty by twenty array of thee ints". Bam - you just malloc()ed 2.4k of memory.
PaulMurrayCbr:
foo[row * nCols + col]
What do those variables mean. I know row, and col, but I have no clue the foo, and I am not 100% sure of the nCols.
They're just names he made up. foo is a generic name that programmers use when they are making something up in the abstract. Replace it with the real name of the array.
You often see simple program examples with 2 functions or two variables or whatever where the the two are named foo and bar. It's sort of like John and Jane Doe when you are speaking of people in the abstract.
nCols is the number of columns. Again, just a made up name to express an idea. You could use Fred as the name of a variable to express the number of columns, but nCols is something you can look at and sort of guess what it is where Fred is not.
Upon further research, I have decided to stick with the first method of just initializing it at the maximum value possible. Thanks for all of the sugestions though! I have run into an issue though with setting/reading data. Here is the code bellow:
float node [30] [7];
float weight [30][6];
void setup(){
Serial.begin(9600);
//creates a random seed based off analog 0 noise
randomSeed(analogRead(0));
setRandWeight(1, 11);
nodeReader();
Serial.println("");
weightReader();
}
//sets a random number from the minimum input to the max
void setRandWeight(int min, int max){
for(int f = 0; f > 30; f++){
for(int s = 0; s > 9; s++){
weight[f][s] = random(min, max);
}
}
}
//reads and prints the matrix to the Serial output
void nodeReader(){
unsigned short temp;
for(int f = 0; f < 20; f++){
Serial.println(" ");
for(int s = 0; s < 10; s++){
temp = node[f][s];
Serial.print("[");
Serial.print(temp);
Serial.print("] ");
}
}
}
//reads the weights that currently are applied in each process
void weightReader(){
unsigned short temp;
for(int f = 0; f < 20; f++){
Serial.println(" ");
for(int s = 0; s < 10; s++){
temp = weight[f][s];
Serial.print("[");
Serial.print(temp);
Serial.print("] ");
}
}
}
For some reason when I run this however, it only displays matrices of [0] for the weight. Is this because when it tries to apply new numbers to the array it overloads the memory and just assigns it all to 0? I didn't think it would do that, but I could be wrong. Any incite would be great!
for(int f = 0; f > 30; f++)
I think you meant less than. Written like this it won't run even once so you're left with the array the same way it started out, full of zeroes.
Oh yeah... I fixed that in the matrix reading for loop, but I must have missed that... Thank you!