I wonder how to locate specific points in outputs in mathematica - mathematica-frontend

The following is my code.
as = Flatten[Import["e:\\matlab\\piturn\\Nor_bms\\as.mat"]];
da = AccountingForm[
Flatten[Import["e:\\matlab\\piturn\\Nor_bms\\da.mat"]]];
For[i = 1, i < 2304, i++, ghdparam = Table[0, {i, 1, 909}, {j, 1, 5}];
For[j = 1, j < 910, j++,
path = "e:\\mathematica\\abs_5s\\" <>
StringTake[ToString[AccountingForm[da[[1, j]]]], 8] <> "\\" <>
as[[i]] <> ".mat";
If[FileExistsQ[path], abs = Flatten[Import[path], 1], Continue[]];
abms = abs[[All, 1]] - abs[[All, 2]];
Print["i=", i];
ghdparam[[j]] =
FindDistributionParameters[abms,
HyperbolicDistribution[\[Lambda], \[Alpha], \[Beta], \[Delta], \
\[Mu]], ParameterEstimator -> "MethodOfMoments"];Print["j="];
Print[j];
Export["e:\\mathematica\\continuous\\" <> as[[i]] <> ".mat",
ghdparam];
Clear[path, abs, abms]]]
This image is part of outputs of my code in mathematica. "i=1" means the first loop in out loop and "j=2" means the second loop in inner loop. And now I want to locate the point with error information. For example, in this picture, only when i=1 and j=5, there is no error information. And then i want to get list of j points with error, just like {1,2,3,4}. for the program will be looped many many times, i want to do this by code. May someone help me! thank you!

Related

Z3py how to solve a problem with many possible path (k out of n potential actions, order matters) efficiently

I am trying to solve a problem that consists of n actions (n >= 8). A path consists k (k == 4 for now) actions. I would like to check if there exists any path, which satisfies the set of constraints I defined.
I have made two attempts to solve this problem:
Attempt 1: Brute force, try all permutations
Attempt 2: Code a path selection matrix M [k x n], such that each row contains one and only one element greater than 0, and all other elements equal to 0.
For instance if k == 2, n == 2, M = [[0.9, 0], [0, 0.7]] represents perform action 1 first, then action 2.
Then my state transition was coded as:
S1 = a2(a1(S0, M[1][1]), M[1][2]) = a2(a1(S0, 0.9), 0)
S2 = a2(a1(S1, M[2][1]), M[2][2]) = a2(a1(S1, 0), 0.7)
Note: I made sure that S == a(S,0), so that in each step only one action is executed.
Then constraints were checked on S2
I was hoping this to be faster than the permutation way of doing it. Unfortunately, this turns out to be slower. Just wondering if there is any better way to solve this problem?
Code:
_path = [[Real(f'step_{_i}_action_{_j}') for _j in range(len(actions))] for _i in range(number_of_steps)]
_states: List[State] = [self.s0]
for _i in range(number_of_steps):
_new_state = copy.deepcopy(_states[-1])
for _a, _p in zip(actions, _path[_i]):
self.solver.add(_a.constraints(_states[-1], _p))
_new_state = _a.execute(_new_state, _p)
_states.append(_new_state)

Why is the Octave conv() giving result different from manual convolution of two signals?

I am trying to write my own code in Octave for convolution of two discreet signals. But when I compared the output with the in-built conv() function, it is coming different. What am I doing wrong? Here is my code:
clc; clear; close all;
[h, fs] = audioread('sound_h.wav');
h = h(1:10000,1);
[x, fs] = audioread('sound_x.wav');
x = x(1:50000,1);
subplot(4, 1, 1)
plot(x);
title("x[n]");
subplot(4, 1, 2)
plot(h);
title("h[n]");
flip_h = fliplr(h);
len_h = length(h);
len_x = length(x);
padded_x = [zeros(len_h-1,1);x;zeros(len_h-1,1)];
y = zeros(len_x+len_h-1,1);
for i = 1:length(y)
y(i) = sum(padded_x(i:i+len_h-1).*flip_h);
endfor
subplot(4, 1, 3)
plot(y);
title("y[n]");
subplot(4, 1, 4)
plot(conv(h, x));
title("y[n] using conv()");
Here are the plots:
The line
flip_h = fliplr(h);
does nothing, because
h = h(1:10000,1);
is a column vector. You need to use flipud in this case.

variables changing value in fortran after calling random_seed

My problem is the following... I have an integer which changes value after calling the random seed function. The value is passed as a parameter and should be 20.
These are the relevant parts of the code:
implicit none
extrenal masres
integer :: i, j, k, n, nc, nr, irch, ncor, ierr, nfil21
...
write(*,*) 'n = ', n
del = 0.d+0
del1 = 0.d+0
call random_seed(size=n)
write(*,*) 'n = ', n
And my output is:
n = 20
n = 33
I tried a workaround which failed too:
write(*,*) 'n = ', n
k = n
del = 0.d+0
del1 = 0.d+0
call random_seed(size=k)
write(*,*) 'k = ', k
allocate(seed(n))
call system_clock(COUNT=clock)
if(info.eq.1) then
seed = clock+37*[(1-i**2/1+i**2,i=1,n)]
else
seed = into+37*[(i+24)**2,i=1,n]
endif
call random_seed(put=seed)
What I get is:
n = 20
k = 33
Fortran runtime error: Array size of PUT is too small.
This is obvious, as the random seed is apparently initialized with 33 instead of 20, so the allocated seed array will be too small.
I don't know if it matters, but I use the gfortran compiler.
Thank you in advance for your help. Tell me if I should post more code or upload the file.

F sharp KMP Algorithm is stuck in the first while loop if i use a pattern with the same characters at the first two indces

I am playing around with the KMP algorithm in f sharp. While it works for patterns like "ATAT" (result will be [|0; 0; 1; 2;|]) , the first while loop enters a deadlock when the first 2 characters of a string are the same and the 3rd is another, for example "AAT".
I understand why: first, i gets incremented to 1. now the first condition for the while loop is true, while the second is also true, because "A" <> "T". Now it sets i to prefixtable.[!i], which is 1 again, and here we go.
Can you guys give me a hint on how to solve this?
let kMPrefix (pattern : string) =
let (m : int) = pattern.Length - 1
let prefixTable = Array.create pattern.Length 0
// i : longest proper prefix that is also a suffix
let i = ref 0
// j: the index of the pattern for which the prefix value will be calculated
// starts with 1 because the first prefix value is always 0
for j in 1 .. m do
while !i > 0 && pattern.[!i] <> pattern.[j] do
i := prefixTable.[!i]
if pattern.[!i] = pattern.[j] then
i := !i+1
Array.set prefixTable j !i
prefixTable
I'm not sure how to repair the code with a small modification, since it doesn't match the KMP algorithm's lookup table contents (at least the ones I've found on Wikipedia), which are:
-1 for index 0
Otherwise, the count of consecutive elements before the current position that match the beginning (excluding the beginning itself)
Therefore, I'd expect output for "ATAT" to be [|-1; 0; 0; 1|], not [|0; 0; 1; 2;|].
This type of problem might be better to reason about in functional style. To create the KMP table, you could use a recursive function that fills the table one by one, keeping track of how many recent characters match the beginning, and start running it at the second character's index.
A possible implementation:
let buildKmpPrefixTable (pattern : string) =
let prefixTable = Array.zeroCreate pattern.Length
let rec run startIndex matchCount =
let writeIndex = startIndex + matchCount
if writeIndex < pattern.Length then
if pattern.[writeIndex] = pattern.[matchCount] then
prefixTable.[writeIndex] <- matchCount
run startIndex (matchCount + 1)
else
prefixTable.[writeIndex] <- matchCount
run (writeIndex + 1) 0
run 1 0
if pattern.Length > 0 then prefixTable.[0] <- -1
prefixTable
This approach isn't in danger of any endless loops/recursion, because all code paths of run either increase writeIndex in the next iteration or finish iterating.
Note on terminology: the error you are describing in the question is an endless loop or, more generally, non-terminating iteration. Deadlock refers specifically to a situation in which a thread waits for a lock that will never be released because the thread holding it is itself waiting for a lock that will never be released for the same reason.

This seems to just be filling up an array with the greatest number, why?

def third_greatest(nums)
idx = 0
arr = []
i = 1
largest = 0
while idx < nums.length
while i < nums.length
if nums[idx] > nums [i]
largest = nums[idx]
else
largest = nums[idx]
end
i += 1
end
arr.push(largest)
idx += 1
i += idx
end
return arr[2]
end
puts(third_greatest([4, 3, 2, 1]) == 2)
#should equal true
I'm trying to get the third largest number out of the array but I keep getting four for any value of the array that returns data.
Any help would be great!
Here is an easier solution for finding the third greatest number in an array:
def third_greatest(nums)
nums.sort!
nums[-3]
end
third_greatest([4, 3, 2, 1])
=> 2
puts(third_greatest([4, 3, 2, 1]) == 2)
=> true
at the end of the first time to the loop, i will be nums.length.
afterwards you increase i with idx so it is now bigger than nums.length.
In the next loops you will never enter the inner loop again, so the largest is never updated anymore.
That's why you always get largest in the result.
to fix it do something like :
end
arr.push(largest)
idx += 1
i = idx + 1
end
so that i is reset to one higher than idx.
But the real solution is to leverage the rich standard library as Alex suggests.
Recent versions of Enumerable#max have allowed a parameter:
(0..9).to_a.max(3).last #=> 7
max(n) returns the three largest values, in decreasing magnitude. This could be expected to be more efficient than sort (unless n == arr.size, of course). Related Enumerable methods (max_by, min, min_by) also have this functionality.

Resources