Why matbugs never stops running? - matlab
I can run a model in WinBUGS14 with no problem and get the results, but I get a problem when I run the same WinBUGS model (shown as below) from MatLab. It looks the program never stops running and no results return.
Can anyone help me. Any advice will be greatly appreciated. Thanks.
1) my WinBUGS Code --- CHK_model.txt
model {
for(i in 1: N) {
CF01[i] ~ dnorm(0, 20)
CF02[i] ~ dnorm(0, 1)
h[i] ~ dpois (lambda [i])
log(lambda [i]) <- beta0 + beta1*CF03[i] + beta2*CF02[i] + beta3*CF01[i] + beta4*IND[i]
}
beta0 ~ dnorm(0.0, 1.0E-6)
beta1 ~ dnorm(0.0, 1.0E-6)
beta2 ~ dnorm(0.0, 1.0E-6)
beta3 ~ dnorm(0.0, 1.0E-6)
beta4 <- log(p)
p ~ dunif(lower, upper)
}
2) my MatLab Code
dataStruct = struct( ...
'N', 155, ...
'lower', 0.80, ...
'upper', 0.95, ...
'h',[1, 4, 1, 2, 1, 2, 1, 1, 1, 3, 3, 0, 0, 0, 2, 0, 1, 0, 4, 2, ...
3, 0, 2, 1, 1, 2, 2, 2, 3, 4, 2, 3, 1, 0, 1, 3, 3, 3, 1, 0, 1, ...
0, 5, 2, 1, 2, 1, 3, 3, 1, 1, 0, 2, 2, 0, 3, 0, 0, 3, 2, 2, 2, ...
1, 0, 3, 3, 1, 1, 1, 2, 1, 0, 1, 2, 1, 2, 0, 2, 1, 0, 0, 2, 5, ...
0, 2, 1, 0, 2, 1, 2, 2, 2, 0, 3, 2, 1, 3, 3, 3, 3, 0, 1, 3, 3, ...
3, 1, 0, 0, 1, 2, 1, 0, 1, 4, 1, 1, 1, 1, 2, 1, 3, 0, 0, 1, 1, ...
1, 1, 0, 2, 1, 0, 0, 1, 1, 5, 1, 1, 1, 3, 0, 1, 1, 1, 0, 2, 1, ...
0, 3, 3, 0, 0, 1, 2, 6, nan], ...
'CF03',[-1.575, 0.170, -1.040, -0.010, -0.750, ...
0.665, -0.250, 0.145, -0.345, -1.915, -1.515, ...
0.215, -1.040, -0.035, 0.805, -0.860, -1.775, ...
1.725, -1.345, 1.055, -1.935, -0.160, -0.075, ...
-1.305, 1.175, 0.130, -1.025, -0.630, 0.065, ...
-0.665, 0.415, -0.660, -1.145, 0.165, 0.955, ...
-0.920, 0.250, -0.365, 0.750, 0.045, -2.760, ...
-0.520, -0.095, 0.700, 0.155, -0.580, -0.970, ...
-0.685, -0.640, -0.900, -0.250, -1.355, -1.330, ...
0.440, -1.505, -1.715, -0.330, 1.375, -1.135, ...
-1.285, 0.605, 0.360, 0.705, 1.380, -2.385, -1.875, ...
-0.390, 0.770, 1.605, -0.430, -1.120, 1.575, 0.440, ...
-1.320, -0.540, -1.490, -1.815, -2.395, 0.305, ...
0.735, -0.790, -1.070, -1.085, -0.540, -0.935, ...
-0.790, 1.400, 0.310, -1.150, -0.725, -0.150, ...
-0.640, 2.040, -1.180, -0.235, -0.070, -0.500, ...
-0.750, -1.450, -0.235, -1.635, -0.460, -1.855, ...
-0.925, 0.075, 2.900, -0.820, -0.170, -0.355, ...
-0.170, 0.595, 0.655, 0.070, 0.330, 0.395, 1.165, ...
0.750, -0.275, -0.700, 0.880, -0.970, 1.155, 0.600, ...
-0.075, -1.120, 1.480, -1.255, 0.255, 0.725, ...
-1.230, -0.760, -0.380, -0.015, -1.005, -1.605, ...
0.435, -0.695, -1.995, 0.315, -0.385, -0.175, ...
-0.470, -1.215, 0.780, -1.860, -0.035, -2.700, ...
-1.055, 1.210, 0.600, -0.710, 0.425, 0.155, -0.525, ...
-0.565], ...
'CF02',[nan, nan, nan, nan, nan, nan, nan, nan, nan, ...
nan, nan, nan, nan, nan, nan, 0.38, 0.06, -0.94, ...
-0.02, -0.28, -0.78, -0.95, 2.33, 1.43, 1.24, 1.26, ...
-0.75, -1.5, -2.09, 1.01, -0.05, 2.48, 2.48, 0.46, ...
0.46, -0.2, -1.11, 0.52, -0.37, 0.58, 0.86, 0.59, ...
-0.12, -1.33, 1.4, -1.84, -1.4, -0.76, -0.23, ...
-1.78, -1.43, 1.2, 0.32, 1.87, 0.43, -1.71, -0.54, ...
-1.25, -1.01, -1.98, 0.52, -1.07, -0.44, -0.24, ...
-1.31, -2.14, -0.43, 2.47, -0.09, -1.32, -0.3, ...
-0.99, 1.1, 0.41, 1.01, -0.19, 0.45, -0.07, -1.41, ...
0.87, 0.68, 1.61, 0.36, -1.06, -0.44, -0.16, 0.72, ...
-0.69, -0.94, 0.11, 1.25, 0.33, -0.05, 0.87, -0.37, ...
-0.2, -2.22, 0.26, -0.53, -1.59, 0.04, 0.16, -2.66, ...
-0.21, -0.92, 0.25, -1.36, -1.62, 0.61, -0.2, 0, ...
1.14, 0.27, -0.64, 2.29, -0.56, -0.59, 0.44, -0.05, ...
0.56, 0.71, 0.32, -0.38, 0.01, -1.62, 1.74, 0.27, 0.97, ...
1.22, -0.21, -0.05, 1.15, 1.49, -0.15, 0.05, -0.87, ...
-0.3, -0.08, 0.5, 0.84, -1.67, 0.69, 0.47, 0.44, ...
-1.35, -0.24, -1.5, -1.32, -0.08, 0.76, -0.57, ...
-0.84, -1.11, 1.94, -0.68], ...
'CF01',[nan, nan, nan, nan, nan, nan, nan, nan, nan, ...
nan, nan, nan, nan, nan, nan, nan, nan, nan, nan, ...
nan, -0.117, -0.211, -0.333, -0.229, -0.272, ...
-0.243, -0.148, 0.191, -0.263, -0.239, -0.168, ...
-0.381, -0.512, -0.338, -0.296, 0.067, 0.104, ...
-0.254, -0.167, -0.526, -0.096, -0.43, 0.013, ...
-0.438, -0.297, -0.131, -0.098, -0.046, -0.063, ...
-0.194, -0.155, -0.645, -0.603, -0.374, -0.214, ...
-0.165, -0.509, -0.171, -0.442, -0.468, -0.289, ...
-0.427, -0.519, -0.454, 0.046, -0.275, -0.401, ...
-0.542, -0.488, -0.52, -0.018, -0.551, -0.444, ...
-0.254, -0.286, 0.048, -0.03, -0.015, -0.219, ...
-0.029, 0.059, 0.007, 0.157, 0.141, -0.035, 0.136, ...
0.526, 0.113, 0.22, -0.022, -0.173, 0.021, -0.027, ...
0.261, 0.082, -0.266, -0.284, -0.097, 0.097, -0.06, ...
0.397, 0.315, 0.302, -0.026, 0.268, -0.111, 0.084, ...
0.14, -0.073, 0.287, 0.061, 0.035, -0.022, -0.091, ...
-0.22, -0.021, -0.17, -0.184, 0.121, -0.192, ...
-0.24, -0.283, -0.003, -0.45, -0.138, -0.143, ...
0.017, -0.245, 0.003, 0.108, 0.015, -0.219, 0.09, ...
-0.22, -0.004, -0.178, 0.396, 0.204, 0.342, 0.079, ...
-0.034, -0.122, -0.24, -0.125, 0.382, 0.072, 0.294, ...
0.577, 0.4, 0.213, 0.359, 0.074, 0.388, 0.253, 0.167], ...
'IND',[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, 1, 1, 1, ...
1, 1, 1, 1, 1, 1, 1, 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, 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, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
Nchains = 3;
clear initStructs;
for i=1:Nchains
S.beta0 = 0;
S.beta1 = 0;
S.beta2 = 0;
S.beta3 = 0;
S.P = 0.9;
initStructs(i) = S;
end
bugsFolder = 'C:\Program Files\winbugs14\WinBUGS14';
[samples, stats] = matbugs(dataStruct, ...
fullfile(pwd, 'CHK_model.txt'), ...
'init', initStructs, ...
'view', 0, 'nburnin', 1000, 'nsamples', 5000, ...
'thin', 10, ...
'monitorParams', {'theta', 'mu_theta', 'sigma_theta'}, ...
'Bugdir', bugsFolder);
# N t, thank you so much for your help. I don't know why I cannot response to you by using 'Post your answer". So I response here:
I download the matbugs.m from a website (http://code.google.com/p/matbugs/) and didn't touch this program at all.
When I run the Matlab code (as shown in my post), the computation goes to WinBUGS interface and stops there. Three WinBUGS windows pop up: the first one is WinBUGS Licence window, the second one is Log window, and the third one is Trap window.
1) here is the Log Window; .... data loaded compile(3) model compiled inits(1,
2) here is the Trap Wind ... (pc=764962F9H, fp=0028FB3CH) (pc=76496D39H, fp=0028FBB4H) (pc=764977C3H, fp=0028FC14H) (pc=76497BC9H, fp=0028FC24H) HostMenus.Loop [00003BDEH] .done BOOLEAN FALSE .f SET {0..5} .n INTEGER 0 .res INTEGER 0 .w HostWindows.Window NIL Kernel.Start [00002B8CH] .code PROCEDURE HostMenus.Loop
3) The program of matbugs.m is too long to post here, but I used exactly same program as this one http://code.google.com/p/matbugs/source/browse/trunk/matbugs.m
Thanks again for your time and for your advice!
Bo
Related
How to render a structured mesh in Octave
I would like to recreate this sort of "cubic" surface rendering in GNU Octave: [From AEG Mesher documentation] How can I achieve this, given a list of coordinates which represent a surface or volume in a structured grid?
Obviously there's probably much more elaborate frameworks out there, but just to demonstrate the concept: the idea that you can only work with triangular patches is incorrect. You can specify your own patches as elaborately as you want. In the following example, I create a simple function which draws a 3D cube 'patch', to be treated as a 'pixel' element in a structured 'mesh'. function drawpatchpixel( x, y, z, c ) Vertices = [ 0, 0, 0 0, 1, 0 1, 1, 0 1, 0, 0 0, 0, 1 0, 1, 1 1, 1, 1 1, 0, 1 ]; Faces = [ 1, 2, 3, 4 # front 2, 6, 7, 3 # top 5, 6, 7, 8 # back 1, 5, 8, 4 # bottom 1, 2, 6, 5 # left 4, 3, 7, 8 ]; # right patch( 'Faces', Faces, 'Vertices', Vertices + [x, y, z], 'facecolor', c ) endfunction Then I simply draw the pixels in the preferred colour: Pixels = { 0, 0, 0, 'r'; # base 1, 0, 0, 'r'; 2, 0, 0, 'r'; 3, 0, 0, 'r'; 4, 0, 0, 'r'; 4, 1, 0, 'r'; 4, 2, 0, 'r'; 4, 3, 0, 'r'; 4, 4, 0, 'r'; 3, 4, 0, 'r'; 2, 4, 0, 'r'; 1, 4, 0, 'r'; 0, 4, 0, 'r'; 0, 3, 0, 'r'; 0, 2, 0, 'r'; 0, 1, 0, 'r'; 0, 0, 1, [0.5, 0.5, 0.5]; # pillars 0, 0, 2, [0.5, 0.5, 0.5]; 0, 0, 3, [0.5, 0.5, 0.5]; 0, 0, 4, [0.5, 0.5, 0.5]; 4, 0, 1, [0.5, 0.5, 0.5]; 4, 0, 2, [0.5, 0.5, 0.5]; 4, 0, 3, [0.5, 0.5, 0.5]; 4, 0, 4, [0.5, 0.5, 0.5]; 4, 4, 1, [0.5, 0.5, 0.5]; 4, 4, 2, [0.5, 0.5, 0.5]; 4, 4, 3, [0.5, 0.5, 0.5]; 4, 4, 4, [0.5, 0.5, 0.5]; 0, 4, 1, [0.5, 0.5, 0.5]; 0, 4, 2, [0.5, 0.5, 0.5]; 0, 4, 3, [0.5, 0.5, 0.5]; 0, 4, 4, [0.5, 0.5, 0.5]; 0, 1, 4, 'g'; # roof 0, 2, 4, 'g'; 0, 3, 4, 'g'; 4, 1, 4, 'g'; 4, 2, 4, 'g'; 4, 3, 4, 'g'; 1, 0, 4, 'g'; 2, 0, 4, 'g'; 3, 0, 4, 'g'; 1, 4, 4, 'g'; 2, 4, 4, 'g'; 3, 4, 4, 'g'; }; NPixels = size( Pixels, 1 ); for i = 1 : NPixels drawpatchpixel( Pixels{i, :} ) endfor axis equal view( 30, 30 ) xlabel( 'x'); ylabel('y'), zlabel('z'); camlight Result:
How to create a Mesh from normals and uv in Unity?
Hi I have this data coming from an API and I know it is a cube: { "position": { "itemSize": 3, "type": "Float32Array", "array": [ 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5, -0.5, -0.5, 0.5, -0.5, -0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, 0.5, -0.5, -0.5, 0.5, -0.5, 0.5, -0.5, -0.5, -0.5, -0.5, -0.5 ], "normalized": false }, "normal": { "itemSize": 3, "type": "Float32Array", "array": [ 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1 ], "normalized": false }, "uv": { "itemSize": 2, "type": "Float32Array", "array": [ 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0 ], "normalized": false } } How can I now create from these data a cube and its material in Unity? Is there a library or a built-in function?
First in general for parsing your JSON data into useful structure you need to have an according class structure and the use e.g. JsonUtility [Serializable] public class DataSet { public int itemSize; public int[] array; public bool normalized; } [Serializable] public class Example { public DataSet position; public DataSet normal; public DataSet uv; } and do var data = JsonUtility.FromJson<Example>(yourJsonString); Next this data is still quite raw and we have to parse it further since we want valid vector values var vertices = new List<Vector3>(); for(var i = 0; i < data.position.array.Length; i+=3) { if(i+2 > data.position.array.Length - 1) break; var x = data.position.array[i]; var y = data.position.array[i+1]; var z = data.position.array[i+2]; vertices.Add(new Vector3(x,y,z)); } And similar for normals and UVs(just that here you use Vector2 instead) Then to the main question: Since you already have positions (I assume vertices), normals and UVs I guess you are wondering where to get the triangles from. UVs don't help here at all since they are only texture coordinates defining how the texture is later mapped on triangles. Also the normals can't really help you here. A normal is just a direction and doesn't provide any helpful information about which three vertices together make up a triangle. To get the triangles would only be possible if you know e.g. that always 3 sequential vertices make up a triangle - meaning your mesh has duplicate vertices. Otherwise I'ld say it is not possible to know that triangles from your given data. In Unity the triangles is simply a flat int[] where always three sequential elements are the indexes of the three vertices from the vertices array. So under the one assumption that they are just consequent something like var triangles = new int[vertices.Length]; for(var i = 0; i < triangles.Length; i++) { triangles[i] = i; } Then you could simply put it all together and create a mesh like var mesh = new Mesh(); mesh.vertices = vertices; mesh.triangles = triangles; // Etc
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)
How to count the elements of a sparse matrix in a certain region?
I have a sparse matrix and want to divide the region into 4 parts, dividing x and y in 2 equidistant pieces and want to calculate the sum of the corresponding values. For the example below, the coordinates x-y each corresponds to [0,16] so the region is a square. There is a sparse matrix in this square, which is symmetrical. I would like to divide the region into smaller squares and sum up the sparse values. Region 0:8,0:8 has 2 elements and their values are both (2,3)=(3,2)=8 so the sum is 16. Summation of the 1st region should give 16, 2nd and 3rd are 36 and the 4th one is 26. x = sparse(16,16); x (3,2) = 8; x (10,2) = 8; x (13,2) = 8; x (14,2) = 4; x (15,2) = 4; x (2,3) = 8; x (10,3) = 4; x (13,3) = 4; x (14,3) = 2; x (15,3) = 2; x (2,10) = 8; x (3,10) = 4; x (13,10) = 4; x (14,10) = 2; x (15,10) = 2; x (2,13) = 8; x (3,13) = 4; x (10,13) = 4; x (14,13) = 2; x (15,13) = 2; x (2,14) = 4; x (3,14) = 2; x (10,14) = 2; x (13,14) = 2; x (15,14) = 1; x (2,15) = 4; x (3,15) = 2; x (10,15) = 2; x (13,15) = 2; x (14,15) = 1; i would rather appriciate a shorter way, rather than writing a line for each sub-square. lets say for 6000 sub-squares one should write 6000 lines?
Let's define the input in a more convenient way: X = sparse([... 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 0, 0, 8, 0, 0, 0, 0, 0, 0, 8, 0, 0, 8, 4, 4 0, 8, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 4, 2, 2 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, 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, 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, 0, 0, 0, 0, 0, 0 0, 8, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 2, 2 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, 0, 0 0, 8, 4, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 2, 2 0, 4, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 1 0, 4, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 1, 0]); For convenience, we first make the array dimensions even. We don't use padarray() for this because this makes the sparse matrix full! sz = size(X); newX = sparse(sz(1)+1,sz(2)+1); padTopLeft = true; % < chosen arbitrarily if padTopLeft newX(2:end,2:end) = X; else % bottom right newX(1:sz(1),1:sz(2)) = X; end %% Preallocate results: sums = zeros(2,2,2); Method #1: accumarray We create a mask of the form: 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 and then use it to sum the appropriate elements of newX: sums(:,:,1) = reshape(... accumarray(reshape(repelem([1,2;3,4], ceil(sz(1)/2), ceil(sz(2)/2)),[],1),... reshape(newX, [],1),... [],#sum) ,2,2); Method #2: blockproc (requires the Image Processing Toolbox) sums(:,:,2) = blockproc(full(newX), ceil(sz/2), #(x)sum(x.data(:))); Several notes: I also tried histcounts2, which is very short, but it only tells you the amount of values in each quadrant, not their sum: [r,c] = find(newX); histcounts2(r,c,[2,2]) I might've overcomplicated the accumarray solution.
Although your question is not very precise and you don't made any efford to find a solution, here is what you are asking.. clear;clc;close; Matrix=rand(20,20); Acc=zeros(1,4); Acc(1)=sum(sum( Matrix(1:size(Matrix,1)/2,1:size(Matrix,2)/2) )); Acc(2)=sum(sum( Matrix((size(Matrix,1)/2)+1:end,1:size(Matrix,2)/2))); Acc(3)=sum(sum( Matrix(1:size(Matrix,1)/2,((size(Matrix,2)/2)+1):end ))); Acc(4)=sum(sum( Matrix((size(Matrix,1)/2)+1:end,((size(Matrix,2)/2)+1):end))); % Verification sum(sum(Matrix)) % <- is the same with sum(Acc) % <- this
You can define any rectangle within the matrix by defining the 4 corners of it. Then use a for loop to process all rectangles. regions = [ 1 8 1 8 9 16 1 8 1 8 9 16 9 16 9 16 ]; regionsum = zeros(size(regions,1),1); for rr = 1:size(regions,1) submat = x(regions(rr,1):regions(rr,2),regions(rr,3):regions(rr,4)); regionsum(rr) = sum(submat(:)); end >> regionsum regionsum = 16 36 36 26 If you mean you want to divide the square matrix into 2^N (N>2) squares of the same size then you can write regions with a for loop. N = 1; % 2^N-by-2^N sub-squares L = size(x,1); dL = L/(2^N); assert(dL==int32(dL),'Too many divisions') segments = zeros(2^N,2); for nn = 1:2^N segments(nn,:) = [1,dL]+dL*(nn-1); end regions = zeros(2^(2*N),4); for ss = 1:2^N for tt = 1:2^N regions((2^N)*(ss-1) + tt,:) = [segments(ss,:),segments(tt,:)]; end end example output with dividing into 16 (N=2) square submatrices: >> regions regions = 1 4 1 4 1 4 5 8 1 4 9 12 1 4 13 16 5 8 1 4 5 8 5 8 5 8 9 12 5 8 13 16 9 12 1 4 9 12 5 8 9 12 9 12 9 12 13 16 13 16 1 4 13 16 5 8 13 16 9 12 13 16 13 16 >> regionsum regionsum = 16 0 12 24 0 0 0 0 12 0 0 8 24 0 8 10 >>
Remove zero rows from a list of list in Scala
I have a list of list in Scala such as: val lst = List(List(60, 0, 1, 2, 3, 28, 0, 0, 0, 0), List(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), List(47, 0, 1, 1, 2, 28, 0, 0, 0, 0)) and I want to remove all zero rows and the result should be like: List(List(60, 0, 1, 2, 3, 28, 0, 0, 0, 0), List(47, 0, 1, 1, 2, 28, 0, 0, 0, 0)) Does Scala list have any built-in method to remove these rows?
You can use filter to keep only items (lists) matching a predicate; The predicate can use exists to check for non-zero elements: lst.filter(_.exists(_ != 0))
#Tzach Zohar answer is perfectly fine but here is another way to approach it. scala> lst.filterNot(xs => xs.forall(_ == 0)) res0: List[List[Int]] = List( List(60, 0, 1, 2, 3, 28, 0, 0, 0, 0), List(47, 0, 1, 1, 2, 28, 0, 0, 0, 0) )