Performance – Optimizing the nested loop to calculate geodetic distance

I segmented an image into N super pixels and created a diagram based on that in which each super pixel is considered a node. The information about neighboring super pixels is saved in glcms Array. The weight between each pair of adjacent super pixels is stored in a matrix W,

Finally, I would like to calculate the geodetic distance between non-neighboring super pixels graphshortestpath Function. The following codes do the above, but are very time consuming. In particular, the last section of the code that calculates the geodetic distance takes more time than expected (more than 15 seconds).

    (rows, columns, numberOfColorChannels) = size(Img);
    (L,N) = superpixels(Img,250);

    %Identifying neighborhood relationships
    glcms = graycomatrix(L,'NumLevels',N,'GrayLimits',(1,N),'Offset',  (0,1;1,0)); %Create gray-level co-occurrence matrix from image
    glcms = sum(glcms,3);    % add together the two matrices
    glcms = glcms + glcms.'; % add upper and lower triangles together, make it symmetric
    glcms(1:N+1:end) = 0;    % set the diagonal to zero, we don't want to see "1 is neighbor of 1"

    data = zeros(N,3);
    for labelVal = 1:N
    redIdx = idx{labelVal};
    greenIdx = idx{labelVal}+numRows*numCols;
    blueIdx = idx{labelVal}+2*numRows*numCols;
    data(labelVal,1) = mean(Img(redIdx));
    data(labelVal,2) = mean(Img(greenIdx));
    data(labelVal,3) = mean(Img(blueIdx));


% Euclidean Distance
for i=1:N
    for j=1:N
        if glcms(i,j)~=0


for i=1:N
    for j=1:N

for i=1:N
    for j=1:N
             W(i,j)=(W_num(i,j))/(W_den(i,i));   % Connectivity Matrix W


s_star_temp=zeros(N);   %temporary variable for geodesic distance measurement
for i=1:N
    for j=1:N
        if W(i,j)==0 & i~=j;
            s_star_temp(i,j)=graphshortestpath(W_sparse,i,j); % Geodesic Distance

The question is how the code can be optimized to be more efficient, i. H. To be less time consuming.

Unity – I have no idea how to calculate faces for my family-generated network

So I make a cave generation program and I have all the cornerstones, but I have no idea how to solve the areas of my network. My code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MapGen : MonoBehaviour
    Mesh mesh;

    int() triangles;

    public int xSize = 20;
    public int zSize = 20;
    public int ySize = 20;
    (Range(0f, 4.5f))
    public float SurfaceLevel = 3.5f;
    Vector3() interest;
    Vector3 old = new Vector3(0, 0, 0);
    public bool ShowAlg = false;
    (Header("Slows down the scene veiw dramaticly when in play mode!"))
    public bool ShowVert = true;

    // Start is called before the first frame update
    void Start()
        mesh = new Mesh();
        GetComponent().mesh = mesh;

    void CreateShape()
        interest = new Vector3((xSize + 1) * (zSize + 1) * (ySize + 1));

        float seed = Random.Range(0.2f, 0.5f);

        for (int x = 0; x <= ySize; x++)
            for (int i = 0, y = 0; y <= zSize; y++)
                for (int z = 0; z <= xSize; z++)
                    float ypn = (Mathf.PerlinNoise(x * seed, z * seed) * 2f);
                    float xpn = (Mathf.PerlinNoise(y * seed, z * seed) * 2f);
                    float zpn = (Mathf.PerlinNoise(x * seed, y * seed) * 2f);

                    if (ypn + xpn + zpn >= SurfaceLevel)
                        interest(i) = new Vector3(x, y, z);


    void SolveFaces()
        triangles = new int(xSize * ySize * zSize * 9);

    void UpdateMesh()

        mesh.vertices = interest;
        mesh.triangles = triangles;

        MeshCollider meshc = gameObject.AddComponent(typeof(MeshCollider)) as MeshCollider;
        meshc.sharedMesh = mesh;

    private void OnDrawGizmos()
        if (interest == null)
        for (int i = 0; i < interest.Length; i++)
            if (ShowVert == true)
                Gizmos.color = new Color (0.286f, 0.486f, 0.812f);
                Gizmos.DrawSphere(interest(i), 0.2f);
            if(ShowAlg == true)
                Gizmos.color =;
                Gizmos.DrawLine(old, interest(i));
                old = interest(i);  

This script is placed on an empty game object with a mesh filter, a renderer and a collider.
Note that they do nothing without their faces.
I set it up so that the face array runs on it triangles Variable and missing part of the script should go into the SolveFaces() Function. Thank you in advance.

color – How do you calculate the ISO of the color negative film?

I have in Kodak's publication & # 39; Basic Photographic Sensitometry & # 39; read about the process
Workbook & # 39 ;.

My understanding of the process for black and white films using the example of these films is that you:

  1. Find the Dmin value and add 0.1 – call this A
  2. Find A's antilog
  3. Then film speed = 800 (mililux seconds) / Antilog from A.


Dmin = 0.8

A = Dmin + 0.10 = 0.9

Antilog from A = 8

Film sensitivity = 800 lux seconds / 8 = 100

So an ISO rating of 100.

However, color negatives have three emulsion layers that are sensitive to different light colors. If one looks at the sensitometric curves for a certain film, it usually shows that each color layer has a different Dmin value.

How is the ISO calculated in this case?

The reason I'm asking is that I have a 100 foot roll of Kodak Vision 3 5254 digital intermediate film (I know it is not meant to be recorded), but I would like to find out the approximate ISO and it load in 35 mm cassettes.

bitcoin core – calculate transaction confirmations

I'm writing an API for one of my systems that can check if a monitored address is receiving Bitcoin. I can already check if the address is on the newly received block. Store them in a database for future use as UTXO. It is managed fairly well.

I just ran into a problem. How can I calculate the transaction confirmations stored in my database (without using an external API) since raw blocks do not contain height information? It doesn't make sense in my architecture that RPC requires it.

Python – Calculate the hand value in a blackjack hand

This feature has no really obvious inefficiencies, so I'm assuming we're trying to reduce every possible clock cycle …

Sometimes using Python's built-in aggregation functions is faster than a for Loop. Here's how you could use it sum and any:

def value(self) -> int:
    value = sum(card.value for card in cards)
    if value <= 11 and any(card.rank == "A"):
        value += 10
    return value

Note the order of the and Expression to ensure that the any Iteration only takes place if the value condition is already fulfilled!

If you have flexibility in displaying the cards, you can try that rank on Enum with integer values. I would expect that comparing two integer enumerations is just a snap than comparing two strings.

Python – Calculate the hand value in the blackjack hand

I built a blackjack simulator in Python. I have to increase the execution time performance. I used a profiler to identify the key drivers in execution time.
The following function is responsible for approx. 15% of the total runtime.

def value(self):
    value = 0
    has_ace = False

    for card in
        value += card.value

        if card.rank == "A":
            has_ace = True

    if has_ace and value <= 11:
        value += 10

    return value

The above function is used to calculate a hand's score.
A hand can have multiple cards.
A card has a value and a rank.

In BlackJack, an ace can be worth 1 or 11.

Is there a better way?

java – Create a program to calculate the interest rate and total balance – the program consists of

(a) A class that includes the calculation of the interest rate and total balance

The designer should accept the initial balance and interest rate
Returns the amount of interest
Returns the total amount
Set interest rate
Set the opening balance

(b) A test program that uses the class from step (A) to do the following:

Set the interest rate to 5% and the opening balance to 1000
Print the amount of interest and total balance
Increase interest rate to 10%
Print the amount of interest and total balance
Increase the opening inventory to 2000
Print the amount of interest and total balance


public class Account {
    // variables to store total balance and interest rate
    private double total_balance;
    private double interest_rate;
    /* Constructor to set total balance and interest rate value*/
    public Account(double initial_balance,double interest_rate);
        //convert interest_rate to decimal value
    public void setinterest_rate(double interest_balance)
    { this.total_balance=intial_balance;
    /*Return interest value to integer value*/
    public double getinterest_rate()
    { return interest_rate*100.0;
    public double getinterestAmount()
        return total_balance*interest_rate;
    /** The method getTotalBalance returns the total balance */
    public double getTotalBalance()
        return total_balance;


that's what I've got so far, but in blueJ it says it's wrong

presentation – How do you calculate the viewing distance for a print size?

The viewing distance of an image is based on two factors. The first is the diagonal image size and the second is the pixels per inch that are required at this distance to get a sharp image.

The first rule of thumb is that the viewing distance should be 1.5 to 2 times the diagonal length. This gives you an optimal viewing distance for the entire printed size based on the ideal viewing angle of the human eye. However, you need to understand that this may not be optimal for a landscape because the viewer may actually want to pan the image and the size of the features in the image should serve as the basis for this calculation. However, this is an artistic decision based on the composition of your image.

Second, for the image to look good at the distance you choose, there must be enough pixels per inch (ppi) for the eye to see a smooth image that is not pixelated. The minimum ppi required for an acceptable quality print is calculated by dividing 3438 by the viewing distance. Anything above this ppi looks good at the selected distance.

So: Minimum ppi = 3438 / viewing distance

With viewing distance in inches and where 3438 is a constant for human vision that was derived as follows:

1 / ppi = 2 x viewing distance x tan (0.000290888 / 2)

1 / ppi = viewing distance x tan (0.000290888)

ppi = 3438 / viewing distance

where 0.000290888 radians (1 arc minute) is called the "visual acuity angle" and represents how much resolution a person can see.