MATLAB IF VALUE LESS THAN - list

Hi I am writing a matlab code at the moment. I am trying to compare the values in a list to the number 10 and if the value is less than 10 add 1 to the total. However I cannot seem to get the code right. My code so far
tot = 0
for i=1:n
if(x(i)<10)
tot = +1
else
y=0;
end
end
tot
The value I get for tot always = 1 and never increases? Can someone help edit this or if not provide a solution to the problem?

I would agree with the answer mentioned above, that one should avoid for loops for this. There can be a faster solution. Since, he is just interested in the counts, and not value of numbers, so there is no need to index things back.
Given:
a = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
Computing numbers less than 10 (you could put any number here)
answer = sum(a<10);
Good luck!

In languages like MATLAB and R, you really should not use for loops like this, even as an exercise. Each variable can be a vector, and operations can occur on the whole vector at once, rather than element-by-element.
Given:
x = [ 1 2 3 4 11 12 13 14 15 16 ]
To generate a list of all x less than 10 you would say:
x(x<10)
So to count them:
total = length(x(x<10))
No loop needed or wanted!

Related

How to find the number of subsequences with GCD=1 using BOTTOM-UP DP approach?

I came across a question where we are given with a sequence of numbers, and we have to find the number of subsequences with gcd=1.
Eg:
2 3 5 7
count: 11
6 10 15
count: 1
I have solved this question using the top-down approach by the recurrence relation in which we either consider the element or not which goes like -
func(position,current_gcd) = func(position+1,current_gcd) + func(position+1,GCD(curr_gcd,element[position]) ;
Can someone please suggest a tabulation or bottom-up approach for the same..?

Does Eigen "Sparse matrix format" example contains error?

Eigen 3.3.7 documentation for SparceMatrix
http://eigen.tuxfamily.org/dox/group__TutorialSparse.html
seems to contain an error in Sparse matrix format section:
This storage scheme is better explained on an example. The following matrix
0 3 0 0 0
22 0 0 0 17
7 5 0 1 0
0 0 0 0 0
0 0 14 0 8
and one of its possible sparse, column major representation:
Values: 22 7 _ 3 5 14 _ _ 1 _ 17 8
InnerIndices: 1 2 _ 0 2 4 _ _ 2 _ 1 4
OuterStarts: 0 3 5 8 10 12
InnerNNZs: 2 2 1 1 2
If 14 is moved from the third column to the second (i.e. its indices changed from [4,2] to [4,1]), then the first two arrays, Values and InnerIndices, make sense. OuterStarts doesn't seem to be correct for either 14 position, while InnerNNZs makes sense for 14 being in [4,2] element of the matrix, but is inconsistent with Values array.
Is this example incorrect or am I missing something?
In general, what is the best way of figuring out Eigen, besides examining the source code? I normally look at tests and examples, but building most benchmark and tests for sparse matrices results in compilation errors (were these tests written for older version of Eigen and not updated for version 3?)...
The key is that the user is supposed to reserve at least as many entries per column as they need. In this example the user only reserved 2 entries for the second column, so if you were to try to add another entry to that column, it would probably require an expensive reallocation, or at least a complicated shift to "steal" an unused entry from another column. (I have no idea how this is implemented.)
Upon a cursory look at the documentation you linked to, I didn't see anything about moving entries like you're trying to do. I'm not sure that Eigen supports such an operation. (Correct me if I'm wrong.) I'm also not sure why you would want to do that.
Your final question is probably too broad. I'm not an expert at Eigen, but it seems like a mature, powerful, and well-documented library. If you have any specific problems compiling examples, you should post them here or on an Eigen specific forum. Many people at scicomp.SE are well-versed in Eigen and are accommodating.

Part of a sorted array is reversed

Given a sorted array, a part of which is reversed. We are required to completely sort it.
eg :
ip - 2 4 5 7 13 11 9 14 19
op - 2 4 5 7 9 11 13 14 19
It's easy to solve when we know whether the given array is sorted in ascending or descending order.If we don't know the order, how to solve it?
eg:
ip - 19 17 2 6 8 10 1
op - 19 17 10 8 6 2 1
also ambiguity occurs when 1st part of input is in ascending order and remaining is in descending order.In that case any order can be considered for output.
Assume the final result should be ascending. Run your algorithm once.
Check whether the output is really ascending. If yes, we are done; if no, go to 3.
Roll back to original input, assume the final result should be descending, run your algorithm once.
Check whether the output is really descending. If yes, we are done; if no, the input is illegal.

Project Euler # 51 c++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am trying to solve problem 51 in project euler.
Here is the problem statement : Project Euler Problem 51
I tried the approach given below.
Generate all the prime numbers between 2 and 10 power 8 using Sieve of Eratosthenes
Gather all n-digit numbers from sieve (i.e .. first i will get all 3 digits, then 4 digits .. so on and compute on it)
check if the number has repeated digit (as per problem statement we have 2 *s of same digit) . If yes , convert into string
if yes, find the position where it repeats and save it as a key (for ex: 12341 will result 04 as key since 1 is the number which is repeated in 0 and 4th position)
now based on the key , insert into the bucket (Bucket is a multimap which contains key as repeated position (04) and value as the prime number)
For each key in bucket, delete the repeated position . If am taking 04 bucket key , then all the prime numbers in that bucket would have the strings repeated in the positions 0 and 4. Am deleting 0th and 4th position of the string which would result my number (12341) as 234 and insert it into a map , which will store the occurence as (234, 8 being the trimmed number and number of occurences).
Now, if the key 234 is repeated 8 times, find the numbers which got trimmed and resulted 234. Those are the answers.
I ran this algorithm in c++ , and for 7 primes 56003, 56113, 56333, 56443, 56663, 56773, and 56993 is resulting less than a second ..
but , for 8 digits , i crossed 10 power 8 primes and still it didnt result any answer. I believe answer is above that limit.
And when i try to generate primes between 2 and 10 power 9 its aborting , since i am storing all the numbers in vector.
My question is ,
Is there any way by which we can fine tune the above mentioned steps and find the answer,
or i need to think some other way to find the answer .
Note: Just for an example i took 12341.
There is atleast one issue in your brute force solution. You are assuming exactly 2 digits are * but question never mentions this. There may be 1 or 3 or more digits which when replaced with the same digits 0-9 still generate primes.
It is impossible to have 8 prime with 1 or 2 * for the following reason:
If you use just 1 *, and let's say replace it with 1 to get a prime(let's call this prime p). Now if p % 3 = 1, you can not replace * with 0, 3 and 6 otherwise the number would become composite(divisible by 3). Throwing away 3 candidates makes it impossible to generate another prime. Next case if p % 3 = 2 you can not replace * with 2, 5 and 8 for the same reason. Making 8-primes with one * impossible for any number of digits.
If you use just 2 *, and let's say replace both with 1 to get a prime(let's call this prime p). Now if p % 3 = 2, you can not replace both * with 0, 3 and 6 otherwise the number would become composite(divisible by 3). Throwing away 3 candidates makes it impossible to generate another prime. Next case if p % 3 = 1 you can not replace * with 2, 5 and 8 for the same reason. Making 8-primes with two * impossible for any number of digits.
This is the reason why your code does not give the required output. You should perhaps try with 3 * characters.

regexp-like library for matrix pattern search

Is there a library (in any language) that can search patterns in matrixes like regular expressions work for strings ? Something like regular expresions for matrixes, or any matrix pattern search method ?
If you're not averse to using J, you can find out whether two matrices are equal by using the -: (match) operator. For example:
X =: 4 3 $ i.12
X
0 1 2
3 4 5
6 7 8
9 10 11
Y =: 4 3 $ (1+i.12)
Y
1 2 3
4 5 6
7 8 9
10 11 12
X -: X
1
X -: Y
0
One nice feature of the match operator is that you can use it to compare arrays of arbitrary dimension; if A is a 3x3x4 array and B is a 2x1 array, then A-:B returns 0.
To find out whether a matrix is a submatrix of another matrix, you can use the E: (member of interval) operator like so:
X =: 2 2 $ 1 2 4 5
X
1 2
4 5
Y =: 4 3 $ (1+i.12)
Y
1 2 3
4 5 6
7 8 9
10 11 12
X E. Y
1 0 0
0 0 0
0 0 0
0 0 0
The 1 at the top left of the result signifies that the part of Y that is equal to X has the given pixel as its upper left-hand corner. The reason for this is that there may be several overlapping copies of X embedded in Y, and only flagging the one pixel lets you see the location of every matching tile.
I found two things: gawk and a perl script.
It's a different problem because string regular expressions work (e.g., sed, grep) work line-by-line on one-dimensional strings.
Unless your matrices are one-dimensional (basically vectors), these programs and the algorithms they use won't work.
Good luck!
Just search rows of the pattern in each row of the input matrix using Aho-Corasick (time O(matrix size)). The result should be small enough to quickly join it into the final result.
I don't think there exists anything quite like regular expressions for dimensions higher than 1, but if you want to match an exact pattern instead of a class of patterns then I might suggest you read up on convolution (or rather Cross-correlation )
The reason being, there are many highly optimized library functions (eg. IPP) for doing this faster than you could ever hope to achieve on your own. Also this method scales to higher dimensions as well.
Also, this won't necessarily give you a "match", but rather a "peak" in a correlation map which will correspond to the match if that peak is equal to the sum of squared coefficients of the pattern you are searching for.