我有一个大小为(m,1)的向量v,其元素是从1:n中选取的整数.我想创建一个大小为(m,n)的矩阵M,如果v(i)= j则其元素M(i,j)为1,否则为0.我不想使用循环,并希望仅将其实现为简单的向量矩阵操作.
所以我首先考虑创建一个包含重复元素的矩阵
M = v * ones(1,n) % this is a (m,n) matrix of repeated v
Run Code Online (Sandbox Code Playgroud)
例如,v = [1,1,3,2]'m = 4且n = 3
M =
1 1 1
1 1 1
3 3 3
2 2 2
Run Code Online (Sandbox Code Playgroud)
然后我需要创建一个大小为(1,n)的比较向量c
c = 1:n
1 2 3
Run Code Online (Sandbox Code Playgroud)
然后我需要进行一系列逻辑比较
M(1,:)==c % this results in [1,0,0]
.
M(4,:)==c % this results in [0,1,0]
Run Code Online (Sandbox Code Playgroud)
但是,我认为应该可以在紧凑矩阵表示法中执行遍历每一行的最后步骤,但是我很难理解并且对索引不够了解.最终结果应该是
M =
1 0 0
1 0 0
0 0 1
0 1 0
Run Code Online (Sandbox Code Playgroud) 假设我有两个(大)向量a=[0 0 0 0 0]并且b=[1 2 3 4 5]具有相同的大小和一个索引向量ind=[1 5 2 1],其值为{1,...,length(a)}.我想计算
for k = 1:length(ind)
a(ind(k)) = a(ind(k)) + b(ind(k));
end
% a = [2 2 0 0 5]
Run Code Online (Sandbox Code Playgroud)
也就是说,我想将b声明的条目添加ind到a包括多重性.
a(ind)=a(ind)+b(ind);
% a = [1 2 0 0 5]
Run Code Online (Sandbox Code Playgroud)
当然要快得多,但忽略多次出现的指数.
我怎样才能加快上面的代码?
我想对不属于某个范围的数组内容执行指标.
例如,我有一个包含1000行和2列的数组.我想对一列中的所有元素(比如列#2)执行mean()计算,这些元素不属于行50-150,250-300,400-700和900-950.
因此,应该基于行1-49,151-249,301-399,701-899和951-1000计算平均值.
任何想法如何去做?
编辑:我应该指出,每次运行程序时,包含的项目都会更改.因此,我不能仅仅对内含物进行硬编码; 他们需要根据排除情况制定出来.
我有一个有趣的问题.就是这样; 我试图在mex函数中使用std :: memcpy函数,并在MATLAB中调用那个mex函数;
I2 = b_filter(I);
Run Code Online (Sandbox Code Playgroud)
当我复制整个图像时,效果很好;
plhs[0] = mxCreateDoubleMatrix(mxGetM(plhs[0]), mxGetN(plhs[0]), mxREAL);
memcpy(mxGetPr(plhs[0]), mxGetPr(prhs[0]), sizeof(double) *mxGetM(plhs[0]) * mxGetN(plhs[0]));
Run Code Online (Sandbox Code Playgroud)
但是当我试图复制图像的某些部分时
plhs[0] = mxCreateDoubleMatrix(100, 100, mxREAL);
memcpy(mxGetPr(plhs[0]), mxGetPr(prhs[0]), sizeof(double) * 100 * 100);
Run Code Online (Sandbox Code Playgroud)
它没有给出正确的图像部分,但却给出了无意义的像素值.
那么这里发生了什么?
我在R中做了一些优化,与此相关,我需要编写一个返回jacobian的函数.这是一个非常简单的雅各比 - 只有零和一个 - 但我想快速干净地填充它.我目前的代码有效,但非常草率.
我有一个四维概率阵列.索引尺寸i, j, k, l.我的约束是,对于每个i, j, k,索引上的概率总和l必须等于1.
我像这样计算我的约束向量:
get_prob_array_from_vector <- function(prob_vector, array_dim) {
return(array(prob_vector, array_dim))
}
constraint_function <- function(prob_vector, array_dim) {
prob_array <- get_prob_array_from_vector(prob_vector, array_dim)
prob_array_sums <- apply(prob_array, MARGIN=c(1, 2, 3), FUN=sum)
return(as.vector(prob_array_sums) - 1) # Should equal zero
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:什么是计算jacobian的干净,快速的方法as.vector(apply(array(my_input_vector, array_dim), MARGIN=c(1, 2, 3), FUN=sum)) - 即,我constraint_function在上面的代码中 - 关于my_input_vector?
这是我的草率解决方案(我从numDeriv包中检查了jacobian函数的正确性):
library(numDeriv)
array_dim <- c(5, 4, 3, 3)
get_prob_array_from_vector <- function(prob_vector, array_dim) …Run Code Online (Sandbox Code Playgroud) 我们来定义一下,例如,
x = 10:10:2000;
Run Code Online (Sandbox Code Playgroud)
众所周知,整数值可以用作索引:
>> x(9)
ans =
90
Run Code Online (Sandbox Code Playgroud)
在Matlab中,通常可以在需要数字的地方使用字符,Matlab会自动进行转换.例如,由于ASCII码'a'是97,
>> 'a'+1
ans =
98
Run Code Online (Sandbox Code Playgroud)
字符也可以用作索引吗?Matlab会将它们转换为整数吗?
我有一个关于索引3个暗淡数组的问题.
说我有一个三维数组
x<- c(1:36)
dim(x) <- c(3,4,3)
Run Code Online (Sandbox Code Playgroud)
现在我想根据包含所有[i,j]位置的第三维索引的矩阵从该数组中提取值.
y <- c(rep(1,4),rep(2,4),rep(3,4))
dim(y) <- c(3,4)
y
[,1] [,2] [,3] [,4]
[1,] 1 1 2 3
[2,] 1 2 2 3
[3,] 1 2 3 3
Run Code Online (Sandbox Code Playgroud)
所以结果应该是这样的:
[,1] [,2] [,3] [,4]
[1,] 1 4 19 34
[2,] 2 17 20 35
[3,] 3 18 33 36
Run Code Online (Sandbox Code Playgroud)
有一些优雅的方式来做到这一点?我知道如何使用两个for循环遍历数组,但这对我的数据来说太慢了.
我想通过多个布尔数组而不是循环来索引带有布尔掩码的数组。
这是我想要实现的,但没有循环,只有numpy。
import numpy as np
a = np.array([[0, 1],[2, 3]])
b = np.array([[[1, 0], [1, 0]], [[0, 0], [1, 1]]], dtype=bool)
r = []
for x in b:
print(a[x])
r.extend(a[x])
# => array([0, 2])
# => array([2, 3])
print(r)
# => [0, 2, 2, 3]
# what I would like to do is something like this
r = some_fancy_indexing_magic_with_b_and_a
print(r)
# => [0, 2, 2, 3]
Run Code Online (Sandbox Code Playgroud) 我有一个 3D 数组,它是一个掩码。此外,我有一些索引,用于编码应保存某些值的位置(数组位置)。
一切似乎都工作正常,除了将值分配到所需位置后输出矩阵仍然为空。
我看不到我在这里缺少什么。我也试过numpy.put没有运气。
import numpy as np
# Initialize output matrix (here the results will be stored)
results = np.zeros((67, 67, 45))
# define the mask - where to put the calculated values in the results array
mask = np.random.randint(2, size=(67, 67, 45)).astype(bool)
# store the results only in these positions
index_keep = range(0, 13732)
values = np.ones((13732,))
results[mask][index_keep] = values.copy()
# the results array is still empty
print(results.sum())
#0
Run Code Online (Sandbox Code Playgroud) 我可以对 numpy 数组进行就地反向排序(降序排序),但我还需要能够稍后对其进行取消排序(撤消)。
给出一个未排序的例子:
a = np.array([-1, -2, 1, -3, 2, 0])
Run Code Online (Sandbox Code Playgroud)
我试过:
i = a[::-1].argsort().argsort() # BAD attempt to store original index
# i = array([3, 5, 0, 4, 1, 2])
a[::-1].sort() # in-place reverse sort (works correctly)
# a= array([ 2, 1, 0, -1, -2, -3])
a = a[i] # FAILS to restore original a
# a = array([-1, -3, 2, -2, 1, 0])
Run Code Online (Sandbox Code Playgroud)
上面的方法不起作用。正确的方法是什么i?假设数组非常大,因此我们不想制作任何不必要的副本。