python - Optimisation of a numpy script to create links between points in a specific format and order - Stack Overflow

Starting from a situation like the one below (see image):So, to start with, we have a series of points

Starting from a situation like the one below (see image):

So, to start with, we have a series of points located on the x axis (the distance between the points can vary and the number of points can vary. In the example, we have 5 starting points). We perform a translation along y in several steps (in the example in 3 steps) to arrive at a final y value (in this case 3 units). The numbers in yellow correspond to their indexes.

My aim is to achieve the following:

In other words, to create links between the points that follow the translation. In practice, and to keep things simple, I'd like to obtain a final array like this:

final : 
 [[ 0  6] [ 6 12] [12 18]
 [ 1  7] [ 7 13] [13 19]
 [ 2  8] [ 8 14] [14 20]
 [ 3  9] [ 9 15] [15 21]
 [ 4 10] [10 16] [16 22]
 [ 5 11] [11 17] [17 23]]

Here's the script I made that does the job:


import numpy as np
# u: number of points along the x axis
u = 6
# v: number of points along the y axis
v = 4

def x_links(u,v):
    matrice = np.arange(u*v).reshape((v, u))
    links =np.array([])
    for i in range(u):
        a = np.repeat(matrice[:,i],2)[1:-1] 
        links=np.append(links,a).astype(int)
    return links.reshape(int(len(links)/2),2)
    
example = x_links(u,v)
print("final : \n",example)

My questions are:

  • Can anyone think of a better way of achieving the same result? a much more pythonic script ...
  • In particular, would it be possible to do without using a loop?

Thank you very much.

Starting from a situation like the one below (see image):

So, to start with, we have a series of points located on the x axis (the distance between the points can vary and the number of points can vary. In the example, we have 5 starting points). We perform a translation along y in several steps (in the example in 3 steps) to arrive at a final y value (in this case 3 units). The numbers in yellow correspond to their indexes.

My aim is to achieve the following:

In other words, to create links between the points that follow the translation. In practice, and to keep things simple, I'd like to obtain a final array like this:

final : 
 [[ 0  6] [ 6 12] [12 18]
 [ 1  7] [ 7 13] [13 19]
 [ 2  8] [ 8 14] [14 20]
 [ 3  9] [ 9 15] [15 21]
 [ 4 10] [10 16] [16 22]
 [ 5 11] [11 17] [17 23]]

Here's the script I made that does the job:


import numpy as np
# u: number of points along the x axis
u = 6
# v: number of points along the y axis
v = 4

def x_links(u,v):
    matrice = np.arange(u*v).reshape((v, u))
    links =np.array([])
    for i in range(u):
        a = np.repeat(matrice[:,i],2)[1:-1] 
        links=np.append(links,a).astype(int)
    return links.reshape(int(len(links)/2),2)
    
example = x_links(u,v)
print("final : \n",example)

My questions are:

  • Can anyone think of a better way of achieving the same result? a much more pythonic script ...
  • In particular, would it be possible to do without using a loop?

Thank you very much.

Share Improve this question edited Nov 17, 2024 at 17:11 hpaulj 232k14 gold badges255 silver badges381 bronze badges asked Nov 17, 2024 at 12:51 CertesCertes 1591 silver badge8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

This should work:

matrix = np.arange(u*v).reshape((v,u))
np.array(np.stack((matrix, np.roll(matrix, v-1, axis = 0)), axis=-1))[:-1]

Output:

array([[[ 0,  6],
        [ 1,  7],
        [ 2,  8],
        [ 3,  9],
        [ 4, 10],
        [ 5, 11]],

       [[ 6, 12],
        [ 7, 13],
        [ 8, 14],
        [ 9, 15],
        [10, 16],
        [11, 17]],

       [[12, 18],
        [13, 19],
        [14, 20],
        [15, 21],
        [16, 22],
        [17, 23]]])

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745634611a4637314.html

相关推荐

  • 论文检测,文章检测,降AI率的工具

    1:文字滚筒鸭文字滚筒鸭作为市面上少见的全免费平台,文字滚筒鸭支持文章 AI 率检测、头条文章原创度分析、作文质量评估等多元场景,甚至能一键对比原文与改写后的相似度,所有功能统统 0 收费!精准算法秒出检测结果,不用充值会员,也无需分享裂变

    1小时前
    20
  • UnoCSS即时按需引用的原子css引擎

    什么是unocss?很多前端可能第一次听说这个词unocss是一个即时的原子CSS引擎,它可以让你用简短的类名来控制元素的样式,而不需要写复杂的CSS代码。当然,原子样式也有很多选择,最著名的就是 Tailwind。但由于Tailwind

    1小时前
    20
  • 怎么用html写出哆啦A梦?

    用HTML和CSS来画哆啦A梦(Doraemon)是一项有趣且具有挑战性的任务。虽然HTML和CSS主要用于网页布局和样式,但通过巧妙的组合和定位,可以创建出相对简单的图形和图案。下面是一个简单的示例,展示了如何用HTML和CSS绘制哆啦A

    1小时前
    00
  • 谷歌云第三方SSH工具登录与一键重装系统

    一,设置 root 密码 先选择从浏览器打开 ssh 连接服务器切换到 root 账号,输入代码:sudo -i设置 root 密码,输入代码&

    1小时前
    00
  • 文章降 AI 痕迹方法与工具速览

    文章降AI的方法和工具汇总‌如下: 这几天又认真研究类了一下,想让 AI 生成的文章更自然,摆脱程式化痕迹,可尝试以下方法。借助 GPT、文字滚筒鸭,朱雀大模型检测器、豆包、kimi 等大模型,输入文本后,它们能通过调整结构、替换同义词等操

    1小时前
    00
  • MySQL8.4创建keyring给InnoDB表进行静态数据加密

    参考文档:.4enkeyring-plugin-installation.html.4enkeyring-hashicorp-plugin.html#keyring-hashicorp-vault-configuration1.首先

    1小时前
    10
  • 群体遗传三剑客第三篇:megacc和ggtree进化树分析

    大家好,我是邓飞。之前计划写群体结构三剑客的博文,写了两篇了:搞起来!群体遗传三剑客:PCA、Admixture、进化树群体遗传三剑客第一篇:分组和不分组的PCA分析,添加解释百分比群体遗传三剑客第二篇:Admixture群体结构分析今天介

    1小时前
    00
  • AI生态暗战升级,科技巨头铁幕下的终极博弈

    一场围绕AI与智能体的标准、协议及生态的暗战已然蓄势待发。在美剧《权力的游戏》中,不到终局,主角归属始终成谜。如今的AI行业,正上演着同样扣人心弦的戏码。这并非是传统意义上的军事或政治博弈,而是一场围绕AI与智能体的标准、协议及生态展开的暗

    1小时前
    00
  • 聊聊Spring AI Alibaba的ObsidianDocumentReader

    序本文主要研究一下Spring AI Alibaba的ObsidianDocumentReaderObsidianDocumentReadercommunitydocument-readersspring-ai-alibaba-star

    57分钟前
    00
  • PackML over OPC UA

    在当今数字化转型的浪潮中,制造业正面临着前所未有的挑战与机遇。如何实现设备之间的高效通信与集成,成为提升生产效率、降低成本的关键。OPC UA(OPC Unified Architecture)与PackML(Packaging Machi

    55分钟前
    00
  • 1.8w字图解Java并发容器: CHM、ConcurrentLinkedQueue、7 种阻塞队列的使用场景和原理

    文章多图且内容硬核,建议大家收藏上一章《1.6w 字图解 Java 并发:多线程挑战、线程状态和通信、死锁;AQS、ReentrantLock、Condition 使用和原理》,我们开启了 Java 高并发系列的学习,透彻理解 Java 并

    52分钟前
    00
  • 面试官:从三万英尺角度谈一下Ceph架构设计(1)

    把面试官当陪练,在找工作中才会越战越勇大家好我是小义同学,这是大厂面试拆解——项目实战系列的第3篇文章,如果有误,请指正。本文主要解决的一个问题,Ceph为例子 如何描述项目的架构。一句话描述:主要矛盾发生变化10年前的技术和方案,放到10

    41分钟前
    00
  • 电脑开机会默认一件GHOST

    关于电脑开机会自己重装系统 前段时间电脑一开机就遇到会自己ghost的问题,而且一直再重复同样的操作,我点击restart的时候到开启页面又会自动ghost,而且此页面停留

    35分钟前
    00
  • OWASP TOP10

    什么是OWASP?它的全称是 Open Web Application Security Project(开放式 Web 应用程序 安全 项目)TOP 10OWASP Top 10的意思就是10项最严重的Web 应用程序安全风险列表 ,它总

    30分钟前
    00
  • Prometheus配置docker采集器

    Prometheus 配置 Docker 采集器Prometheus 是一个开源的监控系统和时间序列数据库,广泛用于容器化环境中。通过监控 Docker 容器,用户可以实时获取服务性能、资源使用情况等信息。本文将介绍如何为 Docker 容

    27分钟前
    00
  • MongoDB “升级项目” 大型连续剧(2)

    上期写的是非必要不升级,想到这个事情,有一些事情的仔细琢磨琢磨,为什么数据库升级的事情在很多公司都是一个困扰,从一个技术人的观点,升级是一件好事,功能提升了,性能提升了,开发效率和一些数据库使用的痛点也被解决了,为什么就不愿意升级呢?如果只

    17分钟前
    00
  • UCB的硅光MEMS OCS控制技术

    昨天写的旭创与nEye合作了一个64端口硅光OCS一、光电路交换技术概述(一)电交换与分组交换电路交换的概念源于早期的电话交换机,通过物理连接实现设备间的通信,就像早期打电话时,接线员手动连接线路一样。而分组交换则是

    11分钟前
    00
  • 推荐一个轻量级的监控平台并且支持移动端

    简介XUGOU 是基于Cloudflare构建的轻量化监控平台,专精于系统资源监控与可视化状态页面服务。该平台提供英文简体中文双语支持,满足全球化部署需求。面向开发者及中小团队,项目致力于提供高可用性的监控解决方案。核心功能与实现平台功能

    9分钟前
    00
  • 拥抱国产化:转转APP的鸿蒙NEXT端开发尝鲜之旅

    本文由转转技术团队赵卫兵分享,原题“鸿蒙新篇章:转转 APP 的 HarmonyOS Next 开发之旅”,下文进行了排版优化和内容修订。1、引言2023 年在华为开发者大会(HDC.Together)上,除了面向消费者的 HarmonyO

    3分钟前
    00
  • maxwell遇到的一则问题

    结论和原因maxwell的元数据库里面没有存储全部的schema数据(就是少数据了),导致相关表的DDL校验失败。PS:我这里maxwell的作用只是采集库表修改情况的统计粗粒度指标,因为之前maxwell在运行报错的时候,直接修改了pos

    1分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信