Posts

Showing posts with the label loops

Extrapolating value from function to a for loop and passing it back to the function

Extrapolating value from function to a for loop and passing it back to the function function Test() a = 2; b = 1; c = 0.5; q = 0.001; r = 10; function F = Useful(x) %calculates existing values for x with size 11 eq1 = (1*(0.903*x(2))^(-1))-(0.903*x(1)); eq2 = (1*(0.665*x(3))*(0.903*x(2))^(-1))-0.903*x(4); eq3 = (1*(0.399*x(5))*(0.903*x(2)))-0.665*x(6); eq4 = (1*(0.399*x(5))*(0.903*x(2))^2)-0.903*x(7); eq5 = (1*(0.399*x(5))*(0.903*x(2))^3)-1*x(8); eq6 = (1*(0.665*x(3))*(0.399*x(5))*(0.903*x(2)))-1*x(9); eq7 = (1*(0.665*x(3))*(0.399*x(5))*(0.903*x(2))^2)-0.903*x(10); eq8 = (1*(0.665*x(3))*(0.399*x(5)))-0.903*x(11); eq9 = x(3)+x(4)+x(9)+x(10)+x(11)-a; eq10 = x(5)+x(6)+x(7)+x(8)+x(9)+x(10)+x(11)-b; eq11 = x(2)+x(6)+2*x(7)+3*x(8)+x(9)+2*x(10)-x(1)-x(4)-c; F = [eq1;eq2;eq3;eq4;eq5;eq6;eq7;eq8; eq9; eq10; eq11]; end Value(1,1) = 0; for d = 2:100 x = fsolve(@Useful,x0,options); %Produces the x(1) to x(11) values Value(1,d) = (x(3)+x(5))*d+Value(1,d-1); %Gives a new value after eac...

Taking strings as a 2-D char array

Taking strings as a 2-D char array I want a code such that I enter some strings one-by-one (by pressing enter) and display it. for example; Input abc def Output abc def also I want this input to be in a array so that I can select any character from the array whenever I want. For example: s[1][1] gives 'e'. I have writen a code for this problem. #include <stdio.h> #include <stdlib.h> int main() { int i, j, n, m; scanf("%d%d", &n, &m); char a[n][m]; for (i = 0; i<n; i++) scanf("%s", a[i]); for (i = 0; i<n; i++) { printf("%s", a[i]); printf("n"); } } But for this code my input/output goes like this: Input ab cd Output abcd cd Can anyone tell where am I going wrong? The input you have shown in question is missing the input value of n and m . Show the input value you are giving to n and m . – H.S. Jun 2...

Julia loops are as slow as R loops

Image
Julia loops are as slow as R loops The code below in Julia and R is to show that the estimator of the population variance is a biased estimator, that is it depends on the sample size and no matter how many times we average over different observations, for small number of data points it is not equal to the variance of the population. It takes for Julia ~10 seconds to finish the two loops and R does it in ~7 seconds. If I leave the code inside the loops commented then the loops in R and Julia take the same time and if I only sum the iterators by s = s + i+ j Julia finishes in ~0.15s and R in ~0.5s. s = s + i+ j Is it that Julia loops are slow or R became fast? How can I improve the speed of the code below for Julia? Can the R code become faster? Julia: using Plots trials = 100000 sample_size = 10; sd = Array{Float64}(trials,sample_size-1) tic() for i = 2:sample_size for j = 1:trials res = randn(i) sd[j,i-1] = (1/(i))*(sum(res.^2))-(1/((i)*i))*(sum(res)*sum(res)) ...

if statement to skip certain values in loop

if statement to skip certain values in loop I have a range of X and Y values, and I have a domain that I don't want to calculate the values of nodes within. I want to have an if statement within a loop in Matlab to skip these. X Y if For example: X = [1:20] Y = [1:20] X = [5:7] Y = [12:14] I think the code should be something like this: for X=1:20 for Y=1:20 if X=2:7 & Y=12:14 return end % the operation here ! T(X,Y) = lab lab lab end end However, I'm not sure how to properly write the condition. 1 Answer 1 You could use ismember or a combination of any and == for the condition, and continue is the command for skipping to the next loop index... ismember any == continue % ismember example for X = 1:20 for Y = 1:20 if ismember( X, 2:7 ) && ismember( Y, 12:14 ) continue end % your loop...

Adding looping within VideoView

Adding looping within VideoView I'm having a bit of trouble with a small application I've put together in Android Studio. Essentially the application is suppose to launch - autoplay a video and keep looping until touched... I've got everything bar the looping working - I've tried a few suggestions from here but none have worked in my case (or not had the coding skills to get them to...) Main code below package com.pixel.danny.screensaverhfx; import android.app.Activity; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.VideoView; public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); VideoView videoView = findViewById(R.id.videoView); Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.hab); vid...

Finding the sum of numbers in an array - excluding the number 13 and the number directly after it

Finding the sum of numbers in an array - excluding the number 13 and the number directly after it I would like to write a program in Java which, given an array, finds the sum of all the numbers in the array - with an exception! Since the number 13 is very unlucky, I propose that we shall completely exclude the number 13, and the number directly after 13, if it exists, from the total sum. The program, which I shall call sum13 , should produce the following results from the following inputs (these are just a few examples): sum13 sum13([1,2,2,1]) = 6 This one is normal; no 13's here. sum13([1,2,2,1]) = 6 sum13([5, 13, 2]) = 5 The 13 and the number directly after the 13 are excluded. sum13([5, 13, 2]) = 5 sum13([13, 13]) = 0 The array contains only 13's, so neither of them are included. sum13([13, 13]) = 0 sum13([1, 2, 13, 2, 1, 13]) = 4 A slightly longer example of the expected output. sum13([1, 2, 13, 2, 1, 13]) = 4 Here is the code which I came up with for sum13 : sum13 pu...

Is it advisable to perform additional logic on data in the front-end?

Is it advisable to perform additional logic on data in the front-end? When receiving data to display in the front end is it advisable to perform additional logic on that data in the front-end - e.g. checking columns if they contain specific values, then hiding the columns, or should this be done in the backend which should return to front-end just a boolean value? My worry is that when the data grows it will drastically affect performance of the app in the browser. If yes what arguments should be used in discussion with people who do not agree / don't want to find time to do it? I'm, using Angular ngFor to display data, but obviously I need to perform that logic before building the DOM. 2 Answers 2 Yes, It's advisable to do that.But there are few cases to consider: Scenario 1: There are ~million customers and each customer holds data of ~1000 rows. In this scenario, its better we do most...