作为字符串的外积? [英] Outer product as string?

查看:55
本文介绍了作为字符串的外积?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下操作.数组 [a,b; 的外积;c,d] 本身可以描述为长度为 2 的字符串"的 4x4 数组.因此,在 4x4 矩阵的左上角,值是 aa、ab、ac、ad.在 numpy/python 或 matlab 中生成这些字符串的最佳方法是什么?

I am trying to do the following. The outer product of an array [a,b; c,d] with itself can be described as a 4x4 array of 'strings' of length 2. So in the upper left corner of the 4x4 matrix, the values are aa, ab, ac, ad. What's the best way to generate these strings in numpy/python or matlab?

这只是一个外部产品的示例.目标是处理k个连续的外积,即4x4矩阵可以再次乘以[a,b;c,d] 等等.

This is an example for just one outer product. The goal is to handle k successive outer products, that is the 4x4 matrix can be multiplied again by [a,b; c,d] and so on.

推荐答案

您可以使用 np.char.array() 以更简单的方式获得@Jaime 的结果:

You can obtain @Jaime's result in a much simpler way using np.char.array():

a  = np.char.array(list('abcd'))
print(a[:,None]+a)

给出:

chararray([['aa', 'ab', 'ac', 'ad'],
       ['ba', 'bb', 'bc', 'bd'],
       ['ca', 'cb', 'cc', 'cd'],
       ['da', 'db', 'dc', 'dd']],
      dtype='|S2')

这篇关于作为字符串的外积?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆