matlab structures finding duplicates in one field to search through another field - matlab

I have a large structure with many fields but I need to find the index of the min magnitude at each time interval.
Structure(:).Time = [ 1, 1, 1, 1, 1, 11, 11, 21, 21, 21, 31, 31, 31, 31, 31, ...]
Structure(:).Mag = [ 11, 16, 9, 4, 6, 111, 10, 8, 15, 3, 0, 95, 52, 16, 7, ...]
So the solution should be:
Solutionindex = [ 4, 7, 10, 11, ...]
To correspond with time = 1, Mag = 4; time = 11, Mag = 10; time = 21, Mag = 3; time = 31, Mag = 0.

This sounds like a job for accumarray (and its trusty sidekick unique)!
% Sample data:
Structure = struct('Time', { 1, 1, 1, 1, 1, 11, 11, 21, 21, 21, 31, 31, 31, 31, 31}, ...
'Mag', {11, 16, 9, 4, 6, 111, 10, 8, 15, 3, 0, 95, 52, 16, 7});
[timeVals, ~, index] = unique([Structure(:).Time]); % Find an index for unique times
nTimes = cumsum(accumarray(index, 1)); % Count the number of each unique time
Solutionindex = accumarray(index, [Structure(:).Mag].', [], #(x) find(x == min(x), 1)) + ...
[0; nTimes(1:(end-1))];
And the result:
Solutionindex =
4
7
10
11

With unique, you can get the different time intervals, then some logic and find. In find the second argument is number of indices to return. This is set to 1 to return the first index. If the last index is wanted, add , 'last' behind the 1.
Time = [ 1, 1, 1, 1, 1, 11, 11, 21, 21, 21, 31, 31, 31, 31, 31];
Mag = [ 11, 16, 9, 4, 6, 111, 10, 8, 15, 3, 0, 95, 52, 16, 7];
[uniques,idx] = unique(Time);
Solutionindex = zeros(1,length(uniques));
for ii=1:length(uniques)
Solutionindex(ii) = find(Mag(Time==uniques(ii)) == min(Mag(Time==uniques(ii))),1)+idx(ii)-1;
end
Result:
Solutionindex =
4 7 10 11

Related

How do I compare List<List<int>> to exist in other List<List<int>>

I want to know in Flutter if my sequence exists in the other sequenceTwo. For the example, I made two identical List<List<int>> that work. But I also want to know if it exists even if there are more record before or after the sequence as in the next check I do in the code below.
List<List<int>> sequence = [];
sequence.add(Uint8List.fromList([42, 6, 1, 8, 6, 63, 13, 10]));
sequence.add(Uint8List.fromList([42, 6, 1, 8, 9, 66, 13, 10]));
sequence.add(Uint8List.fromList([42, 6, 1, 8, 0, 57, 13, 10]));
sequence.add(Uint8List.fromList([42, 6, 1, 8, 3, 60, 13, 10]));
List<List<int>> sequenceTwo = [];
sequenceTwo.add(Uint8List.fromList([42, 6, 1, 8, 6, 63, 13, 10]));
sequenceTwo.add(Uint8List.fromList([42, 6, 1, 8, 9, 66, 13, 10]));
sequenceTwo.add(Uint8List.fromList([42, 6, 1, 8, 0, 57, 13, 10]));
sequenceTwo.add(Uint8List.fromList([42, 6, 1, 8, 3, 60, 13, 10]));
Function deepEqOne = const DeepCollectionEquality().equals;
debugPrint(
'DeepCollection: ' + deepEqOne(sequence, sequenceTwo).toString()); // true so works!
sequenceTwo.add(Uint8List.fromList([42, 6, 1, 9, 3, 60, 13, 10]));
debugPrint(
'DeepCollection: ' + deepEqOne(sequence, sequenceTwo).toString()); // false but I want this also to work somehow

How to manage the hidden state dims when using pad_sequence?

Using Pytorch LSTM architecture trying to build a text generation model. For every batch, I'm using pad_sequence to have min padding for every sequence, therefore I have a variable dims batch (batch_size * seq_len). I'm applying also pack_padded_seq to only give the non-zero (non-padding) tokens to the LSTM. But, the variable dims batch throws an error while feeding it to the LSTM as following; Expected hidden[0] size (1, 8, 16), got (1, 16, 16). In this error, I have provided batch size 16 with 8 tokens for every sequence, but the hidden state is 16 * 16.
I have tried to create the hidden state in the forward function, but that did not work well. How can I create the hidden state such that it can accept variable dims batch and it will not be lost for the whole epoche?
class RNNModule(nn.Module):
def __init__(self, n_vocab, seq_size, embedding_size, lstm_size):
super(RNNModule, self).__init__()
self.seq_size = seq_size
self.lstm_size = lstm_size
self.embedding, num_embeddings, embedding_dim = create_emb_layer(weight_matrix, False)
self.lstm = nn.LSTM(embedding_size,
lstm_size,
num_layers=flags.n_layers,
batch_first=True
)
self.dense = nn.Linear(lstm_size, n_vocab)
def forward(self, x,length,prev_state):
embed = self.embedding(x)
packed_input = db.pack_src(embed,length)
packed_output, state = self.lstm(packed_input,prev_state)
padded,_ = db.pad_pack(packed_output)
logits = self.dense(padded)
return logits, state
def zero_state(self, batch_size = flags.batch_size):
return (torch.zeros(flags.n_layers, batch_size, self.lstm_size),
torch.zeros(flags.n_layers, batch_size, self.lstm_size))
input: tensor([[ 19, 9, 4, 3, 68, 8, 6, 2],
[ 19, 9, 4, 3, 7, 8, 6, 2],
[ 3, 12, 17, 10, 6, 40, 2, 0],
[ 4, 3, 109, 7, 6, 2, 0, 0],
[ 188, 6, 7, 18, 3, 2, 0, 0],
[ 4, 3, 12, 6, 7, 2, 0, 0],
[ 6, 7, 3, 13, 2, 0, 0, 0],
[ 3, 28, 17, 69, 2, 0, 0, 0],
[ 6, 3, 12, 11, 2, 0, 0, 0],
[ 3, 13, 6, 7, 2, 0, 0, 0],
[ 3, 6, 7, 13, 2, 0, 0, 0],
[ 6, 3, 23, 7, 2, 0, 0, 0],
[ 3, 28, 10, 2, 0, 0, 0, 0],
[ 6, 3, 23, 2, 0, 0, 0, 0],
[ 3, 6, 37, 2, 0, 0, 0, 0],
[1218, 2, 0, 0, 0, 0, 0, 0]])
Zero tokens are padding.
Embedding size: 64
LSTM size: 16
batch size: 16
The size of the hidden state you create has the correct size, but your input does not. When you pack it with nn.utils.rnn.pack_padded_sequence you've set batch_first=False, but your data has size [batch_size, seq_len, embedding_size] when you pass it to the packing, so that has batch_size as the first dimension. Also for the LSTM you use batch_first=True, which is appropriate for your data.
You only need to pack it correctly by setting batch_first=True as well, to match the order of your data.
rnn_utils.pack_padded_sequence(embed,length,batch_first=True)

modifying google ortools vrptw example fails

I done some experiments with this exmalple:
https://developers.google.com/optimization/routing/vrptw
My Data set is this:
data['time_windows'] = [(0, 1), (0, 84), (0, 84), (0, 84), (0, 84), (0, 84), (12, 36), (12, 36)]
data['time_matrix'] = [[0, 8, 7, 8, 4, 6, 8, 7], [0, 0, 10, 11, 4, 9, 8, 7], [0, 10, 0, 11, 4, 9, 8, 7], [0, 10, 11, 0, 4, 9, 8, 7], [0, 10, 11, 4, 0, 9, 8, 7], [0, 10, 11, 4, 9, 0, 8, 7], [0, 10, 11, 4, 9, 8, 0, 7], [0, 10, 11, 4, 9, 8, 7, 0]]
I want to have only one vehicle for a test. But if I set " data['num_vehicles'] = 1 " I get no output but a clean exit. Why? I guess this is just a standard TSP, if I reduce the vehicles to 1.
Well, as mentioned by Laurent Perron in the comment, there was a maximum time per vehicle in this example.
routing.AddDimension(
transit_callback_index,
30, # allow waiting time
30, # maximum time per vehicle <<<<<<<<<<<<<<<<<<<
False, # Don't force start cumul to zero.
time)
I changed it to a bigger number (99) and all the crap works very fine :)

Getting "invalid type character" error with daisy

I have a data frame with mixed data types (integer, character, and logical) which I'm trying to cluster with daisy.
I'm using:
gower_dist <- daisy(relchoice, metric = "gower")
and getting:
Error in daisy(relchoice, metric = "gower") :
invalid type character for column numbers 3, 4, 5, 7, 8, 10, 13, 14, 15, 16,
21, 29, 31, 32invalid type character for column numbers 3, 4, 5, 7, 8, 10,
13, 14, 15, 16, 21, 29, 31, 32invalid type character for column numbers 3,
4, 5, 7, 8, 10, 13, 14, 15, 16, 21, 29, 31, 32invalid type character for
column numbers 3, 4, 5, 7, 8, 10, 13, 14, 15, 16, 21, 29, 31, 32invalid type
character for column numbers 3, 4, 5, 7, 8, 10, 13, 14, 15, 16, 21, 29, 31,
32invalid type character for column numbers 3, 4, 5, 7, 8, 10, 13, 14, 15,
16, 21, 29, 31, 32invalid type character for column numbers 3, 4, 5, 7, 8,
10, 13, 14, 15, 16, 21, 29, 31, 32invalid type character for column numbers
3, 4, 5, 7, 8, 10, 13, 14, 15, 16, 21, 29, 31, 32invalid type character for
column numbers 3, 4, 5, 7, 8, 10, 13, 14, 15, 16, 21, 29, 31, 32invalid type
character for column numbers 3, 4, 5, 7, 8, 10, 13, 14, 15, 16, 21, 29, 31,
32
Would love some help with this.
I was able to fix this problem by converting categorical fields to a factor datatype, for example:
df$job <- as.factor(df$job)

Histogram from two vectors in Matlab

Thanks in advance for the help.
I have two sets of parallel vectors:
x = [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, 55];
x_count = [7721, 6475, 3890, 2138, 1152, 784, 674, 492, 424, 365, 309, 302, 232, 250, 220, 208, 190, 162, 144, 134, 97, 93, 89, 97, 92, 85, 77, 87, 64, 75, 72, 82, 61, 48, 46, 44, 35, 20, 28, 20, 21, 10, 6, 8, 4, 4, 4, 3, 1, 1];
y = [1, 2, 3, 4, 5, 6, 7, 8, 9, 55];
y_count = [88, 40, 24, 12, 8, 5, 1, 1, 1, 100];
where x, y are the categories, and x_count, y_count are the frequency of each categories. x and y can be of unequal lengths, and need not contain the same categories.
I want to create a side-by-side bar/histogram plot, where the x-axis is the categories, placed side-by-side like this: side by side multiply histogram in matlab. The frequency counts go along the y-axis.
I've tried googling around, but still stuck on this. If someone could help, that would be great. The solution in side by side multiply histogram in matlab works only if x and y have the same length, but mine's not.
You can try this:
% create unique bins
bins = unique([x y]);
% create vectors with zeros same size as bins
xBins = zeros(size(bins));
yBins = zeros(size(bins));
% fill in counts in the respective spots
xBins(ismember(x, bins)) = x_count;
yBins(ismember(y, bins)) = y_count;
bar(bins, [xBins' yBins']);