I have got a PowerShell script which adds items consisting of two items to a collection
$feedList = New-Object Collections.Generic.List[PSCustomObject]
foreach (...) {
$endpointId = ...
$messagehandler = ...
$feedItem = [Int]$endpointId,[Int]$messagehandler
$feedList.Add($feedItem)
}
$feedList = $feedlist | Sort -Unique
Unfortunately this seems to sort alphabetically rather than numerically. eg
219 20
221 59
222 59
225 67
240 78
246 90
46 34
47 31
49 96
51 47
52 103
52 113
52 18
52 20
52 26
52 74
52 76
52 81
55 10
58 18
59 64
60 17
61 52
69 63
70 30
75 12
89 14
90 28
93 1
Since you define $feedList as a generic list of custom objects I'd recommend actually creating custom objects and then sorting them by property.
foreach (...) {
...
$feedItem = [PSCustomObject]#{
'EndpointId' = [int]$endpointId
'MessageHandler' = [int]$messagehandler
}
$feedList.Add($feedItem)
}
$feedList = $feedList | Sort -Unique EndpointId, MessageHandler
Related
I have a matrix A of size 2500 x 500. I want to sum each 10 columns and get the result as a matrix B of size 2500 x 50. That is, the first column of B is the sum of the first 10 columns of A, the second column of B is the sum of second 10 columns of A, and so on.
How can I do that without a for loop? Since I have to do that hundreds of times and it is highly time consuming to do that using for loop.
First, we "block reshape" A, such that we have the desired number of columns. Therefore, we shamelessly steal the code from the great Divakar, and put in some minimal effort to generalize it. Then, we just need to sum along the second axis, and reshape to the original form.
Here's an example with five columns to be summed:
% Sample input data
A = reshape(1:100, 10, 10).'
[r, c] = size(A);
% Number of columns to be summed
n_cols = 5;
% Block reshape to n_cols, see https://stackoverflow.com/a/40508999/11089932
B = reshape(permute(reshape(A, r, n_cols, []), [1, 3, 2]), [], n_cols);
% Sum along second axis
B = sum(B, 2);
% Reshape to original form
B = reshape(B, r, c / n_cols)
That's the output:
A =
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
B =
15 40
65 90
115 140
165 190
215 240
265 290
315 340
365 390
415 440
465 490
Hope that helps!
This can be done with splitapply. An advantage of this approach is that it works even if the group size does not divide the number of columns (the last group is smaller):
A = reshape(1:120, 12, 10).'; % example 10×12 data (borrowed from HansHirse)
n_cols = 5; % number of columns to sum over
result = splitapply(#(x)sum(x,2), A, ceil((1:size(A,2))/n_cols));
In this example,
A =
1 2 3 4 5 6 7 8 9 10 11 12
13 14 15 16 17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70 71 72
73 74 75 76 77 78 79 80 81 82 83 84
85 86 87 88 89 90 91 92 93 94 95 96
97 98 99 100 101 102 103 104 105 106 107 108
109 110 111 112 113 114 115 116 117 118 119 120
result =
15 40 23
75 100 47
135 160 71
195 220 95
255 280 119
315 340 143
375 400 167
435 460 191
495 520 215
555 580 239
We're trying to verify valid input for the parameter PaperSize of Set-PrintConfiguration.
We're trying to create an array with all possible accepted values for the argument:
$testCommand = Get-Command Set-PrintConfiguration
$testCommand.Parameters.PaperSize
$testPaperSie = [Microsoft.PowerShell.Cmdletization.GeneratedTypes.PrinterConfiguration.PaperSizeEnum]
$testPaperSie.DeclaredFields.Name
This does return a list of options but it also includes a value like value__ which does not seem to be suggested by intellisense. This makes me think the query for valid values is incorrect.
To get the possible values from the PaperKind enum, you can do something like this:
function Get-Enum {
param (
[type]$Type
)
if ($Type.BaseType.FullName -ne 'System.Enum') {
Write-Error "Type '$Type' is not an enum"
}
else {
[enum]::GetNames($Type) | ForEach-Object {
$exp = "([$Type]::$($_)).value__"
[PSCustomObject] #{
'Name' = $_
'Value' = Invoke-Expression -Command $exp
}
}
}
}
Get-Enum System.Drawing.Printing.PaperKind
On my machine it returns:
Name Value
---- -----
Custom 0
Letter 1
LetterSmall 2
Tabloid 3
Ledger 4
Legal 5
Statement 6
Executive 7
A3 8
A4 9
A4Small 10
A5 11
B4 12
B5 13
Folio 14
Quarto 15
Standard10x14 16
Standard11x17 17
Note 18
Number9Envelope 19
Number10Envelope 20
Number11Envelope 21
Number12Envelope 22
Number14Envelope 23
CSheet 24
DSheet 25
ESheet 26
DLEnvelope 27
C5Envelope 28
C3Envelope 29
C4Envelope 30
C6Envelope 31
C65Envelope 32
B4Envelope 33
B5Envelope 34
B6Envelope 35
ItalyEnvelope 36
MonarchEnvelope 37
PersonalEnvelope 38
USStandardFanfold 39
GermanStandardFanfold 40
GermanLegalFanfold 41
IsoB4 42
JapanesePostcard 43
Standard9x11 44
Standard10x11 45
Standard15x11 46
InviteEnvelope 47
LetterExtra 50
LegalExtra 51
TabloidExtra 52
A4Extra 53
LetterTransverse 54
A4Transverse 55
LetterExtraTransverse 56
APlus 57
BPlus 58
LetterPlus 59
A4Plus 60
A5Transverse 61
B5Transverse 62
A3Extra 63
A5Extra 64
B5Extra 65
A2 66
A3Transverse 67
A3ExtraTransverse 68
JapaneseDoublePostcard 69
A6 70
JapaneseEnvelopeKakuNumber2 71
JapaneseEnvelopeKakuNumber3 72
JapaneseEnvelopeChouNumber3 73
JapaneseEnvelopeChouNumber4 74
LetterRotated 75
A3Rotated 76
A4Rotated 77
A5Rotated 78
B4JisRotated 79
B5JisRotated 80
JapanesePostcardRotated 81
JapaneseDoublePostcardRotated 82
A6Rotated 83
JapaneseEnvelopeKakuNumber2Rotated 84
JapaneseEnvelopeKakuNumber3Rotated 85
JapaneseEnvelopeChouNumber3Rotated 86
JapaneseEnvelopeChouNumber4Rotated 87
B6Jis 88
B6JisRotated 89
Standard12x11 90
JapaneseEnvelopeYouNumber4 91
JapaneseEnvelopeYouNumber4Rotated 92
Prc16K 93
Prc32K 94
Prc32KBig 95
PrcEnvelopeNumber1 96
PrcEnvelopeNumber2 97
PrcEnvelopeNumber3 98
PrcEnvelopeNumber4 99
PrcEnvelopeNumber5 100
PrcEnvelopeNumber6 101
PrcEnvelopeNumber7 102
PrcEnvelopeNumber8 103
PrcEnvelopeNumber9 104
PrcEnvelopeNumber10 105
Prc16KRotated 106
Prc32KRotated 107
Prc32KBigRotated 108
PrcEnvelopeNumber1Rotated 109
PrcEnvelopeNumber2Rotated 110
PrcEnvelopeNumber3Rotated 111
PrcEnvelopeNumber4Rotated 112
PrcEnvelopeNumber5Rotated 113
PrcEnvelopeNumber6Rotated 114
PrcEnvelopeNumber7Rotated 115
PrcEnvelopeNumber8Rotated 116
PrcEnvelopeNumber9Rotated 117
PrcEnvelopeNumber10Rotated 118
Hope that helps
I have written this code in q for solving Euler 18 problem,as described in the link below, using recursion.
https://stackoverflow.com/questions/8002252/euler-project-18-approach
Though the code works, it is not efficient and gets stack overflow at pyramids of sizes greater than 3000. How could I make this code much more efficient.I believe the optimum code can be less than 30 characters.
pyr:{[x]
lsize:count x;
y:x;
$[lsize <=1;y[0];
[.ds.lastone:x[lsize - 1];
.ds.lasttwo:x[lsize - 2];
y:{{max (.ds.lasttwo)[x] +/: .ds.lastone[x],.ds.lastone[x+1]}each til count .ds.lasttwo};
$[(count .ds.lasttwo)=1;y:{max (.ds.lasttwo) +/: .ds.lastone[x],.ds.lastone[x+1]}0;y:y[]];
x[lsize - 2]:y;
pyr[-1_x]]]
}
To properly implement this logic in q you need to use adverbs.
First, to quickly find the rolling maximums you can use the prior adverb. For example:
q)input:(75;95 64;17 47 82;18 35 87 10;20 04 82 47 65;19 01 23 75 03 34;88 02 77 73 07 63 67;99 65 04 28 06 16 70 92;41 41 26 56 83 40 80 70 33;41 48 72 33 47 32 37 16 94 29;53 71 44 65 25 43 91 52 97 51 14;70 11 33 28 77 73 17 78 39 68 17 57;91 71 52 38 17 14 91 43 58 50 27 29 48;63 66 04 68 89 53 67 30 73 16 69 87 40 31;04 62 98 27 23 09 70 98 73 93 38 53 60 04 23)
q)last input
4 62 98 27 23 9 70 98 73 93 38 53 60 4 23
q)1_(|) prior last input
62 98 98 27 23 70 98 98 93 93 53 60 60 23
That last line outputs the a vector with the maximum value between each successive pair in the input vector. Once you have this you can add it to the next row and repeat.
q)foo:{y+1_(|) prior x}
q)foo[input 14;input 13]
125 164 102 95 112 123 165 128 166 109 122 147 100 54
Then, to apply this function over the whole use the over adverb:
q)foo over reverse input
,1074
EDIT: The approach above can be generalized further.
q provides a moving max function mmax. With this you can find "the x-item moving maximum of numeric y", which generalizes the use of prior above. For example, you can use this to find the moving maximum of pairs or triplets in the last row of the input:
q)last input
4 62 98 27 23 9 70 98 73 93 38 53 60 4 23
q)2 mmax last input
4 62 98 98 27 23 70 98 98 93 93 53 60 60 23
q)3 mmax last input
4 62 98 98 98 27 70 98 98 98 93 93 60 60 60
mmax can be used to simplify foo above:
q)foo:{y+1_ 2 mmax x}
What's especially nice about this is that it can be used to generalize to variants of this problem with wider triangles. For example, the triangle below has two more values on each row and from any point on a row you can move to the left, middle, or right of the row below it.
5
5 6 7
6 7 3 9 1
I am having hard time understanding this syntax,
val grid = {
val input = """ 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
|49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
|81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
|52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
|22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
|24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
|32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
|67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
|24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
|21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
|78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
|16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
|86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
|19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
|04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
|88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
|04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
|20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
|20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
|01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 """
.stripMargin
val rows = input.split("\n").map(_.trim)
rows.map(_.split(" ").map(_.toInt))
}
Here grid is of type Array[Array[Int]]> I understand that we are creating a 2D array based on some logic inside {} of grid. But what is this val grid = {}, and how can we do calculations inside of it.
In Scala, everything is an expression, and you can combine any sequence of expressions at basically any position in the program.
So here, we assign something to grid, that something is the content of the {} block expression. You can have arbitrary sequences of expressions in a block expression, so in your example we start by definining two val, mostly to make the code easier to read.
Then the last expression, which perform a map on the previously defined rows is the expression returned by the block. That is the value that will be assigned to grid.
A good reason to do such a thing here, is that both input and rows are only visible in the block where they are defined. That means they will not be polluting the scope where you use grid. It's actually very good style.
As method declarations (def) are allowed to have a Block Expression defined to them, so does a Value Declaration.
If we look into the specification (6.11) under "Block", we can see the definition for a valid block declaration:
A block expression { s1; ……; sn; e} is constructed from a sequence
of block statements s1,…,sn and a final expression e. The
statement sequence may not contain two definitions or declarations
that bind the same name in the same namespace. The final expression
can be omitted, in which case the unit value () is assumed.
The expected type of the final expression e is the expected type of
the block. The expected type of all preceding statements is undefined.
And then goes to specify the definition for a value block declaration:
A locally defined value definition val x: T = e is bound by the
existential clause val x: T
Evaluation of the block entails evaluation of its statement sequence,
followed by an evaluation of the final expression e, which defines
the result of the block.
This shows that block expression is valid for member declaration as well as for methods. This is particularly useful when you have a block of code which is relevant only to the initialization of the member, allowing you to create a more complex initialization sequence.
I have a data-set, in which I want to extract columns 1-3, 7-9, 13-15, all the way to the end of the matrix
As an example, I've used the standard magic function to create a matrix
A=magic(10)
A =
92 99 1 8 15 67 74 51 58 40
98 80 7 14 16 73 55 57 64 41
4 81 88 20 22 54 56 63 70 47
85 87 19 21 3 60 62 69 71 28
86 93 25 2 9 61 68 75 52 34
17 24 76 83 90 42 49 26 33 65
23 5 82 89 91 48 30 32 39 66
79 6 13 95 97 29 31 38 45 72
10 12 94 96 78 35 37 44 46 53
11 18 100 77 84 36 43 50 27 59
I know that I can extract single columns starting at 1, in intervals of 3 with the command:
Aex=a(:,1 : 3 : end)
Aex =
92 8 74 40
98 14 55 41
4 20 56 47
85 21 62 28
86 2 68 34
17 83 49 65
23 89 30 66
79 95 31 72
10 96 37 53
11 77 43 59
Say I want to extract groups of columns instead (e.g. column 1-3, 7-9 etc.).
Is there a way to do this without having to manually point out all the column numbers?
Thanks for your help!
Rasmus
Is this what you are looking for:
Aex = A(:,[1:3 7:9])
?
I am assuming that you would like the result all concatenated into another large matrix?
If that is the case, try this one on for size:
result = A(diag(0:2)*ones(3,floor((size(A,2) - 3)/6) + 1) + ...
ones(3,floor((size(A,2) - 3)/6) + 1)*diag(1:6:(size(A,2)-3)))
That could probably be shortened with some matrix math rules. You could also parameterize the values so that it can be modified to do more than what this problem expects, (and also might make more sense),
a = 3;
b = 6;
result = A(diag(0:a-1)*ones(a,floor((size(A,2) - a)/b) + 1) + ...
ones(a,floor((size(A,2) - a)/b) + 1)*diag(1:b:(size(A,2)-a)))
where a is the size of "group" (length([1 2 3]) = length([7 8 9]) = ... = 3), etc. and b is the column spacing ([1...7...13...] in your example)
If you would like them separated, I put them in cells here, but they can go to wherever you need:
a = 3;
b = 6;
results = {};
for Cols = 1:b:(size(A,2)-a)
results{end+1} = A(:, Cols:(Cols+2));
end
I didn't check the speed of either of these, but I think the first one may be faster. You may want to split it up into terms so it's more readable, I just did it to fit on a single line (which isn't always the best way of writing code).
The simple way to do this:
M = magic(10);
n = size(M,2)
idx = sort([1:3:n 2:3:n 3:3:n])
M(:,idx)
If however, the pattern of removal is simpler than the pattern of colums that you want to keep you could use this instead:
A = magic(10);
B = A;
B(:,4:3:end)=[];
B(:,4:3:end)=[]; %Yes 3x the same line of code.
B(:,4:3:end)=[];