我有一个布尔数组(来自以前的计算),我想从几个矩阵中选择相关的行。这就是为什么我需要正确的索引数组(稍后重用)的原因。这在Matlab和python中很容易,但我并不认为正确的julian方法是正确的。
我知道DataFrames,但我希望找到一种标准的矩阵和数组方法来实现这一点。
在Matlab中,我会说:
n= 9; temp= 1:n; A= 1.0 + temp;
someTest= mod(temp,2) == 0; % just a substitute of a more complex case
% now I have both someTest and A!
inds= find(someTest); Anew= A(inds,:);
% I got inds (which I need)!我得到的结果是:
n= 10; data= Array(1:n); A= 1.0 .+ data;
someTest= rem.(data,2) .== 0;
inds= [xy[2] for xy in zip(someTest,1:length(someTest)) if xy[1]]; # (*)
Anew= A[inds,:];我假设有一些更短的方式来表达上面的短语。在v0.6中有find()函数,但是我还没有很好地理解julia文档(在这方面我是一个非常非常新手)。
发布于 2019-04-23 18:38:41
你的Matlab代码不能工作。A只是一个行向量(1x9矩阵),所以当您尝试执行A(inds, :)时,您会得到一个错误:
>> Anew= A(inds,:)
Index in position 1 exceeds array bounds
(must not exceed 1).但是如果你只是解决这个问题,你可以在Matlab和Julia中以完全相同的方式解决这个问题,使用逻辑索引或常规索引:
Matlab (这次我要确保它是一个矩阵):
n = 9;
temp = (1:n).';
A = temp * (1:4);
inds = mod(temp,2) == 0;
>> A(inds, :) % using logical indices
ans =
2 4 6 8
4 8 12 16
6 12 18 24
8 16 24 32
>> A(find(inds), :) % using regular indices
ans =
2 4 6 8
4 8 12 16
6 12 18 24
8 16 24 32现在,朱莉娅:
n = 9;
temp = 1:n;
A = temp .* (1:4)'; # notice that we're transposing the opposite vector from Matlab
inds = mod.(temp, 2) .== 0; # you can use iseven.(temp) instead
julia> A[inds, :] # logical indices (BitArray)
4×4 Array{Int64,2}:
2 4 6 8
4 8 12 16
6 12 18 24
8 16 24 32
julia> A[findall(inds), :] # regular integer indices
4×4 Array{Int64,2}:
2 4 6 8
4 8 12 16
6 12 18 24
8 16 24 32在本例中,我将使用Julia和Matlab中的逻辑索引。事实上,Matlab linter (在编辑器中)会告诉你应该在这里使用逻辑索引,因为它更快。然而,在Julia中,可能存在使用inds = findall(iseven, temp)更有效的情况,只需跳过逻辑BitArray,就像@hckr所说的那样。
https://stackoverflow.com/questions/55791605
复制相似问题