Skip to main content

Calculate the centroid of a set of vectors

Workflow preview

Workflow preview
100%
Calculate the centroid of a set of vectors preview
Open on n8n.io

Important notice

This workflow is provided as-is. Please review and test before using in production.

1. Workflow Overview

n8n Workflow: Calculate the Centroid of a Set of Vectors Overview This workflow receives an array of vectors in JSON format, validates that all vectors have the same dimensions, and computes the ce...

Best for

  • Engineering automation workflows
  • intermediate n8n builders looking for reusable templates

Tools used

n8n-nodes-base.webhook, n8n-nodes-base.set, n8n-nodes-base.code, n8n-nodes-base.respondtowebhook, n8n-nodes-base.stickynote

Source and attribution

This workflow is cataloged by N8N Workflows and links back to its original n8n.io source page by Mauricio Perera.

Original n8n.io source

1.1 Workflow description

Title
Calculate the centroid of a set of vectors
Workflow name
Calculate the centroid of a set of vectors

n8n Workflow: Calculate the Centroid of a Set of Vectors

Overview

This workflow receives an array of vectors in JSON format, validates that all vectors have the same dimensions, and computes the centroid. It is designed to be reusable across different projects.

Workflow Structure

Nodes and Their Functions:

  1. Receive Vectors (Webhook): Accepts a GET request containing an array of vectors in the vectors parameter.

    • Expected Input: vectors parameter in JSON format.
    • Example Request: /webhook/centroid?vectors=[[2,3,4],[4,5,6],[6,7,8]]
    • Output: Passes the received data to the next node.
  2. Extract & Parse Vectors (Set Node): Converts the input string into a proper JSON array for processing.

    • Ensures vectors is a valid array.
    • If the parameter is missing, it may generate an error.
    • Expected Output Example:
    {
      "vectors": [[2,3,4],[4,5,6],[6,7,8]]
    }
    
  3. Validate & Compute Centroid (Code Node): Validates vector dimensions and calculates the centroid.

    • Validation: Ensures all vectors have the same number of dimensions.
    • Computation: Averages each dimension to determine the centroid.
    • If validation fails: Returns an error message indicating inconsistent dimensions.
    • Successful Output Example:
    {
      "centroid": [4,5,6]
    }
    
    • Error Output Example:
    {
      "error": "Vectors have inconsistent dimensions."
    }
    
  4. Return Centroid Response (Respond to Webhook Node): Sends the final response back to the client.

    • If the computation is successful, it returns the centroid.
    • If an error occurs, it returns a descriptive error message.
    • Example Response:
    {
      "centroid": [4, 5, 6]
    }
    

Inputs

  • JSON array of vectors, where each vector is an array of numerical values.

Example Input

{
  "vectors": [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
  ]
}

Setup Guide

  1. Create a new workflow in n8n.
  2. Add a Webhook node (Receive Vectors) to receive JSON input.
  3. Add a Set node (Extract & Parse Vectors) to extract and convert the data.
  4. Add a Code node (Validate & Compute Centroid) to:
    • Validate dimensions.
    • Compute the centroid.
  5. Add a Respond to Webhook node (Return Centroid Response) to return the result.

Function Node Script Example

const input = items[0].json;
const vectors = input.vectors;

if (!Array.isArray(vectors) || vectors.length === 0) {
  return [{ json: { error: "Invalid input: Expected an array of vectors." } }];
}

const dimension = vectors[0].length;
if (!vectors.every(v => v.length === dimension)) {
  return [{ json: { error: "Vectors have inconsistent dimensions." } }];
}

const centroid = new Array(dimension).fill(0);
vectors.forEach(vector => {
  vector.forEach((val, index) => {
    centroid[index] += val;
  });
});

for (let i = 0; i < dimension; i++) {
  centroid[i] /= vectors.length;
}

return [{ json: { centroid } }];

Testing

  • Use a tool like Postman or the n8n UI to send sample inputs and verify the responses.
  • Modify the input vectors to test different scenarios.

This workflow provides a simple yet flexible solution for vector centroid computation, ensuring validation and reliability.

1.2 Logical Blocks

This catalog entry is organized from the workflow JSON. The node-level section below shows the executable blocks available for review before importing the template.

2. Block-by-Block Analysis

Block 1 - Receive Vectors

Type / Role
n8n-nodes-base.webhook - webhook
Config choices
Version 2

Block 2 - Extract & Parse Vectors

Type / Role
n8n-nodes-base.set - set
Config choices
Version 3.4

Block 3 - Validate & Compute Centroid

Type / Role
n8n-nodes-base.code - code
Config choices
Version 2

Block 4 - Return Centroid Response

Type / Role
n8n-nodes-base.respondToWebhook - respondToWebhook
Config choices
Version 1.1

Block 5 - Sticky Note

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 6 - Sticky Note1

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 7 - Sticky Note2

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

Block 8 - Sticky Note4

Type / Role
n8n-nodes-base.stickyNote - stickyNote
Config choices
Version 1

3. Summary Table

Workflow Calculate the centroid of a set of vectors
Complexity intermediate
Nodes 8
Categories Engineering
Author Mauricio Perera
Published 03 Feb 2025

4. Reproducing the Workflow from Scratch

  1. 1. Download the workflow JSON

    Use the JSON export at /data/workflows/2839/2839.json as the source template for this automation.

  2. 2. Import the template into n8n

    Open n8n, import the downloaded JSON, and review each node before activating the workflow.

  3. 3. Configure credentials and variables

    Replace placeholder credentials, API keys, webhook URLs, account IDs, and environment-specific values with your own settings.

  4. 4. Test with sample data

    Run the workflow manually or in a staging workspace, inspect node output, and confirm downstream systems receive the expected data.

  5. 5. Activate and monitor

    Enable the workflow only after testing, then monitor executions, errors, and rate limits during the first production runs.

5. General Notes & Resources

Review imported nodes carefully before activation. This catalog entry is intended to help you inspect the workflow structure, understand required services, and find related templates faster.

Node names, credentials, schedules, webhook paths, and external service limits may need adjustment for your workspace.

Frequently asked questions

What does Calculate the centroid of a set of vectors do?

n8n Workflow: Calculate the Centroid of a Set of Vectors Overview This workflow receives an array of vectors in JSON format, validates that all vectors have the same dimensions, and computes the ce...

What do I need before importing this workflow?

Review the workflow JSON, configure any required credentials in n8n, and test the automation in a safe workspace before using it in production.

Can I customize this workflow?

Yes. Use the block-by-block analysis and the downloadable JSON to inspect each node, then adjust credentials, prompts, schedules, filters, or destinations for your Engineering use case.