I have a 3D array of int values and I want to search just through one of the subarrays for a specific value. While I could for-loop my way through every possible combination of the below code.
array[numberIwant][1-255[1-255];
That seems like overkill. I've come across the foreach type of for and thought that might be the answer to my quest but either it's not or I don't understand it well enough to get it to work. Could anyone suggest the way this should be done?
The foreach and the for loop will have almost identical processing time for an array of that size.
Although it might seem like overkill it is not, you will need to do a triple nested for loop then have an if statement seeing if the number you want was found.
Related
im trying to do some calculations on an ndarray I got. I want to subtract the mean of each value and then divide by the standard deviation. I want to do this for each individual value. The only way I found out for me to work is going for two for loops and then saving the results into a new array. Since this is a pretty large data set this will take some time and I am sure that there must be a faster way, which I can't seem to figure out.
I am doing a project and I've just now gotten around to optimizing. I was wondering if the community may be able to help.
I have a single array and my goal is to efficiently iterate through the array, taking out elements if they happen to match. (Matching is based on whether or not the function I have created to match them returns true)
My initial thought was to have a nested loop inside a main loop, but I very much want to stray from the O(n^2).
Thanks in advance.
Edit Sorry about that, the matching case is a checking of two Boolean values, two integers, and a double. I forgot to mention that earlier.
I'm trying to create a program that will select the fastest sorting algorithm for a particular array of integers. I'm trying to check off the condition "is almost sorted," and was wondering what common practice to find this in the industry is.
Assume that there is a sorted array available to the coder. The two possible solutions I can think of are:
Loop through both lists simultaneously. Compare values at the index, find the percentage of correctly placed values. I understand that this is pretty quick (just O(N)), but it can be wildly inaccurate... what if everything is shifted by one space? This algorithm will give 0, but insertion sort will take a single run to do order this.
Find how far something is shifted from it's correct position in either direction (w/ wraparound). This seems to be a better solution, but could be pretty slow (O(N^2), since we might have to loop through a sorted list for every unsorted object, which could be corrected A BIT by comparing the value in a while loop).
Are there others? If not, which do I pick?
Thanks!
i am using octave for my project as i cant afford a matlab license, however i have run into a significant roadblock and that is the lack of associative array data structures.
my problem is this:
i have some data in the form of cell arrays containing matrices with each cell array representing a potential solution to a problem. i also have a floating point number that represents the evaluated performance of that solution which i want to put into a map-like data structure with the floating point score as the key, in order to sort the solutions by their performance.
can anyone suggest a simple solution to this problem?
what i have thought about doing so far is making each element part of a two element cell array, with the evaluation score as the first element and the data as the second, and then put those arrays into another cell array, which i then apply some sorting algorithm to, sorting by array{i}{1}.. but this seems like a pretty clunky solution.
does octave have any functionality in this respect that i am just unaware of? or is my clunky solution the only way to achieve this?
any help would be greatly appreciated
thanks
How about keeping the cell array as it is, but create a matrix where the columns are the evaluation score and the index into the cell array. Then you can easily use sortrows on the evaluation score column and use the index to pull the solution from the cell array. I think this should be a simple solution that has the benefit of not rearranging your potentially large set of data.
I have been given an assignment to write a program that reads in a number of assignment marks from a text file into an array, and then counts how many marks there are within particular brackets, i.e. 40-49, 50-59 etc. A value of -1 in the text file means that the assignment was not handed in, and a value of 0 means that the assignment was so bad that it was ungraded.
I could do this easily using a couple of for loops, and then using if statements to check the values whilst incrementing appropriate integers to count the number of occurences, but in order to get higher marks I need to implement the program in a "better" way. What would be a better, more efficient way to do this? I'm not looking for code right now, just simply "This is what you should do". I've tried to think of different ways to do it, but none of them seem to be better, and I feel as if I'm just trying to make it complicated for the sake of it.
I tried using the 2D array that the values are stored in as a parameter of a function, and then using the function to print out the number of occurences of the particular values, but I couldn't get this to compile as my syntax was wrong for using a 2D array as a parameter, and I'm not too sure about how to do this.
Any help would be appreciated, thanks.
Why do you need a couple for loops? one is enough.
Create an array of size 10 where array[0] is marks between 0-9, array[1] is marks between 10-19, etc. When you see a number, put it in the appropriate array bucket using integer division, e.g. array[(int)mark/10]++. when you finish the array will contain the count of the number of marks in each bucket.
As food for thought, if this is a school assignment, you might want to apply other things you have learned in the course.
Did you learn sorting yet? Maybe you could sort the list first so that you are not iterating over the array several times. You can just go over it once, grab all the -1's, and spit out how many you have, then grab all the ones in the next bracket and so on.
edit: that is of course, assuming that you are using a 1d array.