Calculate the Centroid of a Set of Vectors - n8n Workflow | Neura Market

    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:** ```json { "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:** ```json { "centroid": [4,5,6] } ``` - **Error Output Example:** ```json { "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:** ```json { "centroid": [4, 5, 6] } ``` ## Inputs - JSON array of vectors, where each vector is an array of numerical values. ### Example Input ```json { "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 ```javascript 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.

    Platform
    n8n
    Category
    Data & Analytics
    Price
    Free
    Creator
    Mauricio Perera
    • set
    • code
    • webhook
    • stickyNote
    • respondToWebhook
    Back to MarketplaceMore n8n Workflows

    How to import this workflow into n8n

    1. 1Purchase or download the workflow to get the n8n workflow JSON file.
    2. 2In your n8n instance, open Workflows and choose "Import from File" (or paste the JSON with Ctrl+V on the canvas).
    3. 3Open each node marked with a credential warning and connect your own accounts and API keys.
    4. 4Run the workflow once manually to verify the data flow, then toggle it to Active.

    Related Data & Analytics workflows

    • Generate AI viral videos with VEO 3 and upload to TikTokFree
    • Generate a daily multi-asset market report with TwelveData, Groq and Google Sheets
    Free
  1. Build an OpenAI RAG system with document upload, semantic search and cachingFree
  2. Automate Website Content Scraping and SEO Keyword Extraction with GPT-4o-mini and AirtableFree
  3. Process Contact Form Submissions with Validation and MongoDB StorageFree
  4. Add webhook data to a Google SheetFree
  5. More from Mauricio Perera

    • Scrape Product Hunt Using Google Gemini
    • Currency Conversion Workflow
    • Create Dynamic Workflows Programmatically via Webhooks & N8n API
    All Data & Analytics workflows →All n8n workflows →

    Need help customizing this workflow?

    Our automation experts adapt it to your exact stack, data, and process — or build one from scratch.

    Get a Custom Build
    Neura Market
    Neura Market
    Marketplace
    Directories
    Resources

    Marketplace

    • Prompts
    • Workflows
    • Agents Store
    • Workflow Packs
    • Categories
    • Marketplace

    Directories

    • AI Tools Directory
    • ChatGPT
    • Claude
    • Gemini
    • Cursor
    • Grok
    • DeepSeek
    • Perplexity
    • CoPilot
    • Midjourney
    • Stable Diffusion
    • MCP Servers
    • .md Directory
    • All Directories

    Free Tools

    • AI Text Humanizer
    • AI Content Detector
    • Workflow Generator
    • Model Comparison
    • AI Pricing Calculator
    • AI Benchmarks
    • ROI Calculator
    • All Free Tools

    Resources

    • AI News
    • Blog
    • AI Models
    • Integrations
    • Alternatives
    • n8n vs Zapier
    • Make vs Zapier
    • n8n vs Make
    • Resource Library
    • Documentation

    Community

    • AI Jobs
    • AI Events
    • AI Companies
    • Start Selling
    • Sell n8n Workflows
    • Sell AI Agents
    • Sell Prompts
    • Creator Guide
    • Advertise
    • Affiliates

    Company

    • About
    • Contact
    • Help
    • Careers
    • Pricing
    • Terms
    • Privacy
    • License
    • DMCA

    Stay Updated

    Get the latest AI tools and insights delivered to your inbox.

    Neura Market Logoneuramarket

    © 2026 Neura Market. All rights reserved.