问题标签 [bsxfun]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
3116 浏览

matlab - How does Matlab transpose a sparse matrix?

I've been wondering about this question for quite a while but cannot find a reference: How does Matlab transpose a sparse matrix so fast, given that it is stored in CSC (compressed sparse column) format?

Also its documentation verifies the efficiency of sparse matrix transposition:

To do this (accessing row by row), you can transpose the matrix, perform operations on the columns, and then retranspose the result … The time required to transpose the matrix is negligible.

Follow-up (modified as suggested by @Mikhail):

I agree with @Roger and @Milhail that setting a flag is enough for many operations such as the BLAS or sparse BLAS operations in terms of their interfaces. But it appears to me that Matlab does "actual" transposition. For example, I have a sparse matrix X with size m*n=7984*12411, and I want to scale each column and each row:

t = 0.023636 seconds

t = 138.3586 seconds

t = 19.5433 seconds

This result means that accessing column by column is faster than accessing row by row. It makes sense because sparse matrices are stored column by column. So the only reason for the fast speed of column scaling of X' should be that X is actually transposed to X' instead of setting a flag.

Also, if every sparse matrix is stored in CSC format, simply setting a flag cannot make X' in CSC format.

Any comments? Thanks in advance.

0 投票
0 回答
926 浏览

matlab - MATLAB - 用样本数据逼近半球域的积分

渲染方程的积分部分在半球范围内(相对于立体角)执行积分。
我可以使用以下代码生成带有相应被积函数值的样本:

我现在面临的问题是如何对这些样本进行积分或近似?

0 投票
2 回答
3795 浏览

matlab - Efficiently compute many inner products in matlab

I am working on a project for which I need to compute a lot of inner products in high dimensions. I am aware that we should always try to vectorize operations in matlab, but I am not sure how to do this ...

Lets say we have two matrices, A and B of size N x d where N is the number of inner products to compute in d dimensions.

It is easy to implement using for-loops, but I suspect more efficient ways exist. A for-loop implementation might look like this:

Does anyone have ideas how to vectorize this? I guess bsxfun should come into play at some point, but I cannot figure out how to use it for this ...

Thanks in advance!

0 投票
4 回答
861 浏览

matlab - 对非数字数据使用 `bsxfun`

bsxfun非数字数据是否有等价物?

例如,我想比较存储在两个单元格数组中的所有字符串对:

0 投票
2 回答
941 浏览

matlab - 从数组的每一行中减去多个向量(超级广播)

我有一个数据集,Xm x 2,三个向量存储在一个矩阵C = [c1'; c2'; c3']中,即3 x 2。我正在尝试对我的代码进行矢量化,该代码为 中的每个数据点找到最接近X的向量C(平方距离)。我想从 inC的每个向量(行)中减去每个向量(行)X,从而得到的元素和 的元素之间的差异m x 6或矩阵。我当前的实现一次只做一行:3m x 2XCX

我想摆脱这个for循环,只是矢量化整个事情,但是bsxfun(@minus, X, C)在 Octave 中给了我一个错误:

错误:bsxfun:尺寸不一致:300x2 和 3x2

有什么想法可以“超级广播”这两个矩阵之间的减法运算吗?

0 投票
2 回答
339 浏览

performance - 如何在不使用 for 循环且不影响速度的情况下应用元素操作

假设我有一个 RGB 图像矩阵,我想在它上面应用一些空间过滤器。一般来说,我想应用元素操作(请注意,这是一项大学作业,我不允许使用图像处理工具箱中提供的任何内置函数)。我决定将过滤器编写为函数,然后将bsxfun这些函数应用于图像上。

一个简单的例子是:
我想将 50 添加到图像的所有灰度级,然后用 200 替换所有高于 200 的灰度级。这是我的代码:

test是这种形式的函数:

此代码不起作用,因为在第二行“in > a”是一个具有 0 和 1 的矩阵(我的意思是所有元素都不是 1,也不应该是),因此调试器不会分支到if语句中。

你能指导我如何编写这个函数以及如何在图像上应用空间和傅立叶分析,而不影响性能和运行速度吗?

0 投票
3 回答
216 浏览

matlab - MATLAB 更高效地减去矩阵

我将两个矩阵相减。dataClim 是 30 年期间每个月(12 个月)的平均数据。dataAll 是超过 1257 天的每日数据。我需要从20100101到20130611的每个月的日数据中减去平均月数据(t = 1:31是一月,t = 32-57是二月,一直到十二月,然后363:393又是一月) .

此代码有效,但我想知道是否有任何方法可以使其更高效且不那么乏味。我不知道如何编写循环,因为月份的天数从 28 到 31 不等。

我的一个想法是首先将每个月的天数连接在一起,然后为每个月创建 dataAnom。它可能会更慢。

0 投票
1 回答
355 浏览

matlab - 通过 CUBLAS 或 THRUST 在 CUDA 上编程 bsxfun?

我有一个包含 nx1 个项目的向量 V 和一个包含 nxm 个项目的矩阵 M。我想用 CUDA 将 V 与 M 的所有列相加。THRUST 或 CUBLAS 中是否有任何方法可以帮助我解决问题?

0 投票
1 回答
387 浏览

matlab - matlab中矩阵逐元素乘法的替代方法

我正在研究一个大矩阵乘法。我有一个大矩阵 A(至少 5000x5000)和一个列向量 V(5000x1)。在我的代码中,每个 V 都将 A 元素的每一列逐个元素相乘。我用循环做到了

但它太慢了。所以我创建了一个大矩阵,每列分配为 V 使得

它速度很快,但它浪费了太多的内存。当矩阵的大小增加时,它会占用过多的内存。这是使用更少内存但速度更快的任何想法吗?

0 投票
1 回答
610 浏览

matlab - bsxfun 真的是按元素应用的吗?

假设我有以下功能:

我这样调用bsxfun

bsxfun真的是逐个元素,我希望printAndKeepX被调用 4 次,参数分别(x, y)(1, 1),和。但是输出显示它只被调用了一次:(2, 1)(3, 1)(4, 1)(x, y)([1 2 3 4], 1)

为什么?我怎么知道什么是“元素”?

编辑:
文档建议有时被调用的函数可以接收两个标量,有时可以接收一个向量/矩阵和一个标量。我可以确定其中哪些会发生吗?

我对bsxfun.