Here's an example text I'm trying to format:
(9 12 19 38 0 -39 -20 -13 -10)
(7 9 14 29 0 -30 -15 -10 -8)
(4 6 9 19 0 -20 -10 -7 -5)
(2 3 4 9 0 -10 -5 -4 -3)
(0 0 0 0 0 0 0 0 0)
(-3 -4 -5 -10 0 9 4 3 2)
(-5 -7 -10 -20 0 19 9 6 4)
(-8 -10 -15 -30 0 29 14 9 7)
(-10 -13 -20 -39 0 38 19 12 9)
I would like each column to align on digits, (i.e. if there's a minus sign, add extra space).
If I do it like so: C-uM-xalign-regexpRET\([[:digit:]]+\)RETRETRETy
I get this:
(9 12 19 38 0 -3 -2 -1 -1)
(7 9 14 29 0 -3 -1 -1 -8)
(4 6 9 19 0 -2 -1 -7 -5)
(2 3 4 9 0 -1 -5 -4 -3)
(0 0 0 0 0 0 0 0 0 )
(-3 -4 -5 -1 0 9 4 3 2 )
(-5 -7 -1 -2 0 19 9 6 4 )
(-8 -1 -1 -3 0 29 14 9 7 )
(-1 -1 -2 -3 0 38 19 12 9 )
which is very close, but not what I want.
And if I try to modify the expression to include the minus sign, like so: \(-?[[:digit:]]+\)
Then I get this:
(9 1 1 3 0 - - - -)
(7 9 1 2 0 - - - -)
(4 6 9 1 0 - - - -)
(2 3 4 9 0 - - - -)
(0 0 0 0 0 0 0 0 0)
(- - - - 0 9 4 3 2)
(- - - - 0 1 9 6 4)
(- - - - 0 2 1 9 7)
(- - - - 0 3 1 1 9)
Is this a bug, or is there something I don't know?
The text matched by the designated group (usually group 1) is expanded or shrunk, so non-whitespace characters in the group are subject to deletion, as you saw. Unless, that is, justification is enabled, which is indicated by supplying a negative group number to align-regexp:
C-uM-xalign-regexpRET\(\s-*-?\)[0-9]+RET-1RETRETy
If you want, you can align the columns on the ones digits of each number by including the digits in the match group:
C-uM-xalign-regexpRET\(\s-*-?[0-9]+\)RET-1RETRETy
In either case, an extra space will be inserted after each opening parenthesis. I don't see any way to keep align-regexp from doing this, but if you do it often you could wrap it in a command that does the align-regexp, then replaces the regexp "^( " with "(" everywhere in the original region.
Related
I have the following file stored in a string vector.
ratingsTiny.txt...
Jesse
-3 5 -3 0 -1 -1 0 0 5
Shakea
5 0 5 0 5 0 0 0 1
Batool
5 -5 0 0 0 0 0 -3 -5
Muhammad
0 0 0 -5 0 -3 0 0 0
Maria
5 0 5 0 0 0 0 1 0
Alex
5 0 0 5 0 5 5 1 0
Riley
-5 3 -5 0 -1 0 0 0 3
My goal is extract the numbers, preferably column wise, to add them together and get an average rating for each column among the 7 users.
My closest attempt is below, but I can only print the first column and I can't figure out how to iterate through the entirety of the rows to get the rest of the integers.
Any help is very much appreciated.
ourvector<string> ratings;
for (int i = 1; i < ratings.size(); i += 2){
int num = atoi(ratings[i].c_str());
intRatings.push_back(num);
cout << num << endl;
}
I've been trying to implement quick sort and have been having a lot of problems. I even copied a lot of implementations and accepted answers from the net and they ALL crash on odd sized array/vector if you run it enough times (each time I run I run quick sort against random numbers to be sorted... rather than pretend my code works just cuz it can sort one particular set of numbers).
Here is my code and also prints to help debug the error.
template <typename T>
void quickSortMidPivot(vector<T>&vec, size_t left, size_t right)
{
mcount++;
if(right - left < 1)
return;
//crash all the time
//if(left >= right)
// return;
size_t l = left;
size_t r = right;
T pivot = vec[left + ((right-left)/2)];
cout << endl << "PivotValue:" << pivot << endl;
while (l <= r)
{
while (vec[l] < pivot)
l++;
while (vec[r] > pivot)
r--;
if (l <= r) {
cout << endl << "swap:" << vec[l] << "&" << vec[r] << endl;
std::swap(vec[l], vec[r]);
l++;
r--;
for (int i =left; i<=right; i++)
cout << vec[i] << " ";
}
}
cout << endl << "left:" << left << " r:" << r << endl;
cout << "l:" << l << " right:" << right << endl;
if(left < r)
quickSortMidPivot(vec, left, r);
if(l < right)
quickSortMidPivot(vec, l, right);
}
//in main
quickSortMidPivot(dsVector, 0, dsVector.size() - 1);
mcount is a global just so that I can count number of recursive calls. Help figure out most effective implementation...
Here is some debug info.
When run on even sized vector.
Test values are (PRE-SORTING):
8 4 6 5 2 4 1 2
PivotValue:5
swap:8&2
2 4 6 5 2 4 1 8
swap:6&1
2 4 1 5 2 4 6 8
swap:5&4
2 4 1 4 2 5 6 8
left:0 r:4
l:5 right:7
PivotValue:1
swap:2&1
1 4 2 4 2
left:0 r:0
l:1 right:4
PivotValue:2
swap:4&2
2 2 4 4
swap:2&2
2 2 4 4
left:1 r:1
l:3 right:4
PivotValue:4
swap:4&4
4 4
left:3 r:3
l:4 right:4
PivotValue:6
swap:6&6
5 6 8
left:5 r:5
l:7 right:7
# Recursions:5 0
Data Sorted.
Sorted test values are (POST-SORTING):
1 2 2 4 4 5 6 8
Here is case with odd sized array (9). Works 90% of time.
Test values are (PRE-SORTING):
7 7 5 6 5 8 9 5 8
PivotValue:5
swap:7&5
5 7 5 6 5 8 9 7 8
swap:7&5
5 5 5 6 7 8 9 7 8
swap:5&5
5 5 5 6 7 8 9 7 8
left:0 r:1
l:3 right:8
PivotValue:5
swap:5&5
5 5
left:0 r:0
l:1 right:1
PivotValue:8
swap:8&8
6 7 8 9 7 8
swap:9&7
6 7 8 7 9 8
left:3 r:6
l:7 right:8
PivotValue:7
swap:7&7
6 7 8 7
left:3 r:4
l:5 right:6
PivotValue:6
swap:6&6
6 7
left:3 r:2
l:4 right:4
PivotValue:8
swap:8&7
7 8
left:5 r:5
l:6 right:6
PivotValue:9
swap:9&8
8 9
left:7 r:7
l:8 right:8
# Recursions:7 0
Data Sorted.
Sorted test values are (POST-SORTING):
5 5 5 6 7 7 8 8 9
Here is print output for when odd sized (9) vector input causes crash.
Test values are (PRE-SORTING):
8 3 2 3 9 3 8 1 5
PivotValue:9
swap:9&5
8 3 2 3 5 3 8 1 9
left:0 r:7
l:8 right:8
PivotValue:3
swap:8&1
1 3 2 3 5 3 8 8
swap:3&3
1 3 2 3 5 3 8 8
swap:3&3
1 3 2 3 5 3 8 8
left:0 r:2
l:4 right:7
PivotValue:3
swap:3&2
1 2 3
left:0 r:1
l:2 right:2
PivotValue:1
swap:1&1
1 2
swap:2&0
1 0
swap:3&0
1 0
swap:3&1
1 0
swap:5&0
1 0
swap:3&1
1 0
swap:8&0
1 0
swap:8&0
1 0
swap:9&0
1 0
swap:7274596&0
1 0
swap:666050571&0
1 0
swap:369110150&0
1 0
swap:1&0
1 0
swap:1&0
1 0
swap:110&0
1 0
swap:649273354&0
1 0
swap:134229126&0
1 0
swap:3764640&0
1 0
swap:2293216&0
1 0
swap:8&0
1 0
swap:2&0
1 0
swap:649273354&0
1 0
swap:134229127&0
1 0
swap:3764672&0
1 0
swap:3764608&0
1 0
swap:3&0
1 0
swap:649273354&0
1 0
swap:134229127&0
1 0
swap:3764704&0
1 0
swap:3764640&0
1 0
swap:2&0
1 0
swap:649273354&0
1 0
swap:134229127&0
1 0
swap:3764736&0
1 0
swap:3764672&0
1 0
swap:3&0
1 0
swap:649273354&0
1 0
swap:134229127&0
1 0
swap:3764768&0
1 0
swap:3764704&0
1 0
swap:9&0
1 0
swap:649273354&0
1 0
swap:134229127&0
1 0
swap:3764800&0
1 0
swap:3764736&0
1 0
swap:3&0
1 0
swap:6619252&0
1 0
swap:649273354&0
1 0
swap:134229127&0
1 0
swap:3764832&0
1 0
swap:3764768&0
1 0
swap:8&0
1 0
swap:666050571&0
1 0
swap:402664583&0
1 0
swap:3765152&0
1 0
swap:3764800&0
1 0
swap:1&0
1 0
swap:900931609&0
1 0
swap:268446854&0
1 0
swap:2046&0
1 0
swap:2046&0
1 0
swap:649273354&0
1 0
swap:134229140&0
1 0
swap:2293216&0
1 0
swap:3764832&0
1 0
swap:5&0
1 0
swap:11399&0
1 0
swap:3735896&0
1 0
swap:3735896&0
1 0
swap:548610060&1
1 0
swap:50342980&0
1 0
swap:6356944&-1
1 0
swap:3735800&-2
1 0
swap:3735648&0
1 0
swap:3735648&-1
1 0
swap:3768320&0
1 0
swap:32768&1
1 0
I have a problem with the output of the soft demapper function (demodulate_soft_bits) when using 16-QAM and 64-QAM modulation.
here is a portion of the code (64-QAM):
cvec comple;
Modulator_2D qam;
ivec cont_d = "0:1:63";
vec qam_r = "-7 -7 -7 -7 -7 -7 -7 -7 -5 -5 -5 -5 -5 -5 -5 -5 -3 -3 -3 -3 -3 -3 -3 -3 -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 1 1 1 1 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 5 7 7 7 7 7 7 7 7";
vec qam_i = "-7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7 -7 -5 -3 -1 1 3 5 7";
comple.set_length(qam_i.length(),false);
for(int i=0; i<qam_i.length(); i++)
comple(i) = std::complex<double>(qam_r(i)/sqrt((double)42),qam_i(i)/sqrt((double)42));
qam.set(comple, cont_d);
temp = qam.demodulate_soft_bits( symb_recus,channel,1);
symb_recus is the output of a realistic 4x4 MIMO channel
channel is the channel gain
At the output I got a random values which doesn't correspond to the hard output and I got also a succession of 0
exp :
temp : [6.80 3.33 1.64 -14.27 -7.06 -3.51 0 0 0 0 0 0 -3.47 .....]
Hard output : [ 0 1 0 1 1 1 1 1 0 0 1 1 0 ... ]
can you please help me
I am trying to find a string within another string. However, I am trying to match even if one or more character is not matching.
Let me explain with an example :
Let's say I have a string 'abcdefghij'. Now if the string to match is 'abcd',
I could write strfind('abcdefghij', 'abc')
Now, I have a string 'adcf'. Notice that, there is a mismatch in two characters, I would consider it as a match.
Any idea how to do it ?
I know, this is not the most optimal code.
Example :
a='abcdefghijk';
b='xbcx'
c='abxx'
d='axxd'
e='abcx'
f='xabc'
g='axcd'
h='abxd'
i ='abcd'
All these strings should match with a. I hope this example makes it more clear. The idea is, if there is a mismatch of 1 or 2 characters also, it should be considered as a match.
You could do it like this:
A = 'abcdefghij'; % Main string
B = 'adcf'; % String to be found
tolerance = 2; % Maximum number of different characters to tolerate
nA = numel(A);
nB = numel(B);
pos = find(sum(A(mod(cumsum([(1:nA)' ones(nA, nB - 1)], 2) - 1, nA) + 1) == repmat(B, nA, 1), 2) >= nB - tolerance);
In this case it will return pos = [1 3]'; because "adcf" can be matched on the first position (matching "a?c?") and on the third position (matching "?d?f")
Explanation:
First, we take the sizes of A and B
Then, we create the matrix [(1:nA)' ones(nA, nB - 1)], which gives us this:
Output:
1 1 1 1
2 1 1 1
3 1 1 1
4 1 1 1
5 1 1 1
6 1 1 1
7 1 1 1
8 1 1 1
9 1 1 1
10 1 1 1
We perform a cumulative sum to the right, using cumsum, to achieve this:
Output:
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
5 6 7 8
6 7 8 9
7 8 9 10
8 9 10 11
9 10 11 12
10 11 12 13
And use the mod function so each number is between 1 and nA, like this:
Output:
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
5 6 7 8
6 7 8 9
7 8 9 10
8 9 10 1
9 10 1 2
10 1 2 3
We then use that matrix as an index for the A matrix.
Output:
abcd
bcde
cdef
defg
efgh
fghi
ghij
hija
ijab
jabc
Note this matrix has all possible substrings of A with size nB.
Now we use repmat to replicate B down, 'nA rows'.
Output:
adcf
adcf
adcf
adcf
adcf
adcf
adcf
adcf
adcf
adcf
And perform a direct comparison:
Output:
1 0 1 0
0 0 0 0
0 1 0 1
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Summing to the right give us this:
Output:
2
0
2
0
0
0
0
0
0
0
Which are the number of character matches on each possible substring.
To finish, we use find to select the indexes of the matches within our tolerance.
In your code
c=a-b is not valid (Matrix dimensions not same)
If you need at least one match, not in order, (as your example says), you can have something like this :-
>> a='abcdefgh';
>> b='adcf';
>> sum(ismember(a,b)) ~= 0
ans =
1
I'm looking for a J code to do the following.
Suppose I have a list of random integers (sorted),
2 3 4 5 7 21 45 49 61
I want to start with the first element and remove any multiples of the element in the list then move on to the next element cancel out its multiples, so on and so forth.
Thus the output
I'm looking at is 2 3 5 7 61. Basically a Sieve Of Eratosthenes. Would appreciate if someone could explain the code as well, since I'm learning J and find it difficult to get most codes :(
Regards,
babsdoc
It's not exactly what you ask but here is a more idiomatic (and much faster) version of the Sieve.
Basically, what you need is to check which number is a multiple of which. You can get this from the table of modulos: |/~
l =: 2 3 4 5 7 21 45 49 61
|/~ l
0 1 0 1 1 1 1 1 1
2 0 1 2 1 0 0 1 1
2 3 0 1 3 1 1 1 1
2 3 4 0 2 1 0 4 1
2 3 4 5 0 0 3 0 5
2 3 4 5 7 0 3 7 19
2 3 4 5 7 21 0 4 16
2 3 4 5 7 21 45 0 12
2 3 4 5 7 21 45 49 0
Every pair of multiples gives a 0 on the table. Now, we are not interested in the 0s that correspond to self-modulos (2 mod 2, 3 mod 3, etc; the 0s on the diagonal) so we have to remove them. One way to do this is to add 1s on their place, like so:
=/~ l
1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1
(=/~l) + (|/~l)
1 1 0 1 1 1 1 1 1
2 1 1 2 1 0 0 1 1
2 3 1 1 3 1 1 1 1
2 3 4 1 2 1 0 4 1
2 3 4 5 1 0 3 0 5
2 3 4 5 7 1 3 7 19
2 3 4 5 7 21 1 4 16
2 3 4 5 7 21 45 1 12
2 3 4 5 7 21 45 49 1
This can be also written as (=/~ + |/~) l.
From this table we get the final list of numbers: every number whose column contains a 0, is excluded.
We build this list of exclusions simply by multiplying by column. If a column contains a 0, its product is 0 otherwise it's a positive number:
*/ (=/~ + |/~) l
256 2187 0 6250 14406 0 0 0 18240
Before doing the last step, we'll have to improve this a little. There is no reason to perform long multiplications since we are only interested in 0s and not-0s. So, when building the table, we'll keep only 0s and 1s by taking the "sign" of each number (this is the signum:*):
* (=/~ + |/~) l
1 1 0 1 1 1 1 1 1
1 1 1 1 1 0 0 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 0 1 1
1 1 1 1 1 0 1 0 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
so,
*/ * (=/~ + |/~) l
1 1 0 1 1 0 0 0 1
From the list of exclusion, you just copy:# the numbers to your final list:
l #~ */ * (=/~ + |/~) l
2 3 5 7 61
or,
(]#~[:*/[:*=/~+|/~) l
2 3 5 7 61
Tacit iteration is usually done with the conjunction Power. When the test for completion needs to be something other than hitting a fixpoint, the Do While construction works well.
In this solution filterMultiplesOfHead is applied repeatedly until there are no more numbers not either applied or filtered. Numbers already applied are accumulated in a partial answer. When the list to be processed is empty the partial answer is the result, after stripping off the boxing used to segregate processed from unprocessed data.
filterMultiplesOfHead=: {. (((~: >.)# %~) # ]) }.
appendHead=: (>#[ , {.#>#])/
pass=: appendHead ; filterMultiplesOfHead#>#{:
prep=: a: , <
unfinished=: [: -. a: -: {:
sieve=: [: ; [: pass^:unfinished^:_ prep
sieve 2 3 4 5 7 21 45 49 61
2 3 5 7 61
prep 2 3 4 7 9 10
┌┬────────────┐
││2 3 4 7 9 10│
└┴────────────┘
appendHead prep 2 3 4 7 9 10
2
filterMultiplesOfHead 2 3 4 7 9 10
3 7 9
pass^:2 prep 2 3 4 7 9 10
┌───┬─┐
│2 3│7│
└───┴─┘
sieve 1-.~/:~~.>:?.$~100
2 3 7 11 29 31 41 53 67 73 83 95 97