for-loop

Unusual list comprehension behaviour

亡梦爱人 提交于 2020-08-10 22:28:24
问题 I'm trying to port some code from Python to R and I've come across a list comprehension I cannot fully understand. Here is a toy example analogous to the code import numpy as np theta = np.random.rand(5, 2, 2, 3) thetai = theta[0] logp = [theta[np.newaxis, ...] for theta in thetai] If I run and print the results I get: print(logp) [array([[[0.779, 0.461, 0.766], [0.245, 0.189, 0.045]]]), array([[[0.229, 0.288, 0.173], [0.011, 0.541, 0.528]]])] Ok output is a list of two arrays. What I cannot

How to make a single 256*256*N (double) .mat from multiple .png image

点点圈 提交于 2020-08-10 19:35:07
问题 There are 100 PNG images,size=256*256,channel=1 Here is my test code(test for save 2 images in a mat): label = {sprintf('%01d.png\n', 0:100)}; img = regexp(label{:}(1:end-1), '\n', 'split'); F1=im2double(imread(img{1})); F2=im2double(imread(img{2})); label=cat(1,F1,F2);` save('test.mat', 'label') -> The test.mat is 256X256X2 double However,I want to save 100 images in the mat. My idea is F1~100 <=> 1~100.png then cat(1,F1,F2...F100),and save at last. So I try to use eval() in for loop create

How to pause for loop and wait for user click

核能气质少年 提交于 2020-08-10 19:34:08
问题 I'm creating a quiz app where randomly generating tasks, but I don't know how to wait for user click in for loop. I tried to do this: final Integer min=1; final Integer max=100; for (int i = 0; i<5; i++){ button_check3.setVisibility(View.VISIBLE); button_next3.setVisibility(View.INVISIBLE); final int x = new Random().nextInt(max - min)+min; final int y = new Random().nextInt(max-x - min)+min; task3.setText(x + " + " + y + " ="); //set task final int zbroj = x+y; button_check3

Sliding windows - measuring length of observations on each looped window

蓝咒 提交于 2020-08-10 19:32:07
问题 Let's analyse this sample code where zip() is used to create different windows from a dataset and return them in loop. months = [Jan, Feb, Mar, Apr, May] for x, y in zip(months, months[1:]): print(x, y) # Output of each window will be: Jan Feb Feb Mar Mar Apr Apr May Let's suppose that now I want to calculate the respective length percentage between the months used in each window. Example in steps: When returning the first window (Jan Feb), I want to calculate the % length of Jan over the

Sliding windows - measuring length of observations on each looped window

倖福魔咒の 提交于 2020-08-10 19:32:05
问题 Let's analyse this sample code where zip() is used to create different windows from a dataset and return them in loop. months = [Jan, Feb, Mar, Apr, May] for x, y in zip(months, months[1:]): print(x, y) # Output of each window will be: Jan Feb Feb Mar Mar Apr Apr May Let's suppose that now I want to calculate the respective length percentage between the months used in each window. Example in steps: When returning the first window (Jan Feb), I want to calculate the % length of Jan over the

for loop went to super long time (20+ Minutes) iteration when the input has large amount of data

二次信任 提交于 2020-08-10 19:14:08
问题 I have two functions here: int getHighestVal(int n, vector<double> arr) { int highest = 0; for (int i = 0; i < n; i++) { if (arr[i] > arr[highest]) highest = i; } return highest; } vector<int> getRank(int n, vector<double> arr) { vector<int> rank(n); vector<bool> used(n); for (int i = 0; i < n; i++) used[i] = false; int lowestVal = getHighestVal(n, arr); cout << "Pass waypoint lowestVal" << endl; for (int i = 1; i <= n; i++) { //LOOP HERE WENT INFINITE ITERATION for (int j = 0; j < n; j++) {

jinja2 nested for loops two lists

|▌冷眼眸甩不掉的悲伤 提交于 2020-08-09 18:41:50
问题 I have the following code that is not rendering the way I would like on the html front end. {% for image in images %} {% for title in titles %} <div class="card" style="width: 18rem;"> <img src="{{image}}" class="card-img-top" alt="..."> <div class="card-body"> <h5 class="card-title">{{title}}</h5> <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p> <a href="#" class="btn btn-primary">Go somewhere</a> </div> {% endfor %} {%

jinja2 nested for loops two lists

孤人 提交于 2020-08-09 18:41:22
问题 I have the following code that is not rendering the way I would like on the html front end. {% for image in images %} {% for title in titles %} <div class="card" style="width: 18rem;"> <img src="{{image}}" class="card-img-top" alt="..."> <div class="card-body"> <h5 class="card-title">{{title}}</h5> <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p> <a href="#" class="btn btn-primary">Go somewhere</a> </div> {% endfor %} {%

Sum of consecutive pairs in a list including a sum of the last element with the first

怎甘沉沦 提交于 2020-08-04 07:45:50
问题 I have a list of elements like [1,3,5,6,8,7] . I want a list of sums of two consecutive elements of the list in a way that the last element is also added with the first element of the list. I mean in the above case, I want this list: [4,8,11,14,15,8] But when it comes to the addition of the last and the first element during for loop, index out of range occurs. Consider the following code: List1 = [1,3,5,6,8,7] List2 = [List1[i] + List1[i+1] for i in range (len(List1))] print(List2) 回答1: List2

Looping to Create a New Worksheet for each new data in a Row - MS Excel

送分小仙女□ 提交于 2020-08-03 09:22:47
问题 This script is supposed to: 1. Scroll down Column B where the Depts are located. 2. Next, select the entire Row of Data from Col A to Col F 3. Create a New Worksheet with the name of the Dept in Col B 4. Paste that Entire Row that was selected in the newly created worksheet 5. And then, move on to the Next Row until the End of the Data on the Original Data Sheet 6. If the Dept value is different from that of the previous row in Col B, then a New Worksheet is created and the routine begins