如何避免循环减少此代码的计算时间(one solution of my last question):
我希望找到A(1:3,:)
的列向量,其M(4,:)
中的对应值不属于单元格X
的一个向量的一部分(显然不等于其中一个矢量)。如果X
非常大,我会寻找快速解决方案。
M = [1007 1007 4044 1007 4044 1007 5002 5002 5002 622 622;
552 552 300 552 300 552 431 431 431 124 124;
2010 2010 1113 2010 1113 2010 1100 1100 1100 88 88;
7 12 25 15 12 30 2 10 55 32 12];
我直接点A
:
A = [1007 4044 5002 622;
552 300 431 124;
2010 1113 1100 88];
A
包含M(1:3,:)
X = {[2 5 68 44],[2 10 55 9 17],[1 55 6 7 8 9],[32 12]};
[~, ~, subs] = unique(M(1:3,:)','rows');
A4 = accumarray(subs(:),M(4,:).',[],@(x) {x});
%// getting a mask of which columns we want
idxC(length(A4)) = false;
for ii = 1:length(A4)
idxC(ii) = ~any(cellfun(@(x) all(ismember(A4{ii},x)), X));
end
显示我们想要的列
out = A(:,idxC)
结果:
>> out
out =
1007 4044
552 300
2010 1113
已删除列向量[5002;431;1100]
,因为[2;10;55]
中包含X{2} = [2 10 55 9 17]
已删除列向量[622;124;88]
,因为[32 12] = X{4}
另一个例子:,具有相同的X
M = [1007 4044 1007 4044 1007 5002 5002 5002 622 622 1007 1007 1007;
552 300 552 300 552 431 431 431 124 124 552 11 11;
2010 1113 2010 1113 2010 1100 1100 1100 88 88 2010 20 20;
12 25 15 12 30 2 10 55 32 12 7 12 7];
X = {[2 5 68 44],[2 10 55 9 17],[1 55 6 7 8 9],[32 12]};
A = [1007 4044 5002 622 1077;
552 300 431 124 11;
2010 1113 1100 88 20];
结果 :(使用scmg回答)
如果A
根据第一行排序,我得到:(正确结果)
out =
1007 1007 4044
11 552 300
20 2010 1113
如果我不对矩阵A
进行排序,我会得到:(错误的结果)
out =
4044 5002 622
300 431 124
1113 1100 88
应删除列向量A(:,4) = [622;124;88]
,因为[32 12] = X{4}
。
应删除列向量[5002;431;1100]
,因为[2;10;55]
中包含X{2} = [2 10 55 9 17]
答案 0 :(得分:4)
在这种情况下,您不应该尝试消除循环。矢量化实际上伤害了你。
特别是(给你的匿名lambda命名)
issubset = @(x) all(ismember(A4{ii},x))
效率低得离谱,因为它没有短路。用循环替换它。
相同
any(cellfun(issubset, X))
使用与此类似的方法:
idxC = true(size(A4));
NX = numel(X);
for ii = 1:length(A4)
for jj = 1:NX
xj = X{jj};
issubset = true;
for A4i=A4{ii}
if ~ismember(A4i, xj)
issubset = false;
break;
end;
end;
if issubset
idxC(ii) = false;
break;
end;
end;
end;
两个break
语句,尤其是第二个语句,会触发提前退出,这可能会为您节省大量计算。
答案 1 :(得分:4)
Ben Voigt的答案很棒,但是%row vector
for i = 1:3
disp('foo');
end
foo
foo
foo
%column vector
for i = (1:3).'
disp('foo');
end
foo
行是造成问题的那一行:for循环不会以列向量的方式工作:
A4i = A4{ii}.'
只需尝试使用A(:,idxC) =
4044 5002
300 431
1113 1100
,就可以完成工作了!
现在,如果我们看一下输出:
unique
如您所见,最终结果并非我们的预期。
只要subs =
2
2
3
2
3
2
4
4
4
1
1
进行某种排序,sub就不会按照A中相遇的顺序编号,而是按C中相遇的顺序编号(已排序):
unique
因此,你应该通过[C, ~, subs] = unique(M(1:3,:)','rows');
%% rather than [~, ~, subs] = unique(M(1:3,:)','rows');
给出的矩阵而不是A来获得最终输出
输入
>> out = C(idxC,:).'
out =
1007 4044
552 300
2010 1113
然后,要获得最终输出,请输入
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();
if (Session["ObjUser"] == null)
{
Response.Redirect("~/UI/Login.aspx", true);
}
if (HttpContext.Current.Request.UrlReferrer == null)
{
Response.Redirect("~/UI/Login.aspx", true);
}
答案 2 :(得分:3)
拍摄#1
本节列出的方法应该是一种快速且直接方法来解决我们的案例。请注意,由于A
是M
中考虑到第三行的唯一列的矩阵,因此我们将其作为输入跳过,因为我们在内部使用解决方案代码生成它。这也在下一个方法/镜头中保持。这是实施 -
function out = shot1_func(M,X)
%// Get unique columns and corresponding subscripts
[unqrows, ~, subs_idx] = unique(M(1:3,:)','rows');
unqcols = unqrows.'; %//'
counts = accumarray(subs_idx(:),1); %// Counts of each unique subs_idx
%// Modify each cell of X based on their relevance with the fourth row of M
X1 = cellfun(@(x) subs_idx(ismember(M(4,:),x)),X,'Uni',0);
lensX = cellfun('length',X1); %// Cell element count of X1
Xn = vertcat(X1{:}); %// Numeric array version of X
N = max(subs_idx); %// Number of unique subs_idx
%// Finally, get decision mask to select the correst columns from unqcols
sums = cumsum(bsxfun(@eq,Xn,1:N),1);
cumsums_at_shifts = sums(cumsum(lensX),:);
mask1 = any(bsxfun(@eq,diff(cumsums_at_shifts,[],1),counts(:).'),1); %//'
decision_mask = mask1 | cumsums_at_shifts(1,:) == counts(:).'; %//'
out = unqcols(:,~decision_mask);
return
拍摄#2
前面提到的方法可能会遇到瓶颈:
cellfun(@(x) subs_idx(ismember(M4,x)),X,'Uni',0)
因此,为了将表现保持为良好动机,可以将整个过程分为两个阶段。第一阶段可以处理在X
的第四行中没有重复的M
的单元格,这可以使用矢量化方法实现,另一个阶段解决X's
的其余部分使用我们较慢cellfun
方法的单元格。
因此,代码会膨胀一点,但希望性能更好。最终的实现看起来像这样 -
%// Get unique columns and corresponding subscripts
[unqrows, ~, subs_idx] = unique(M(1:3,:)','rows')
unqcols = unqrows.' %//'
counts = accumarray(subs_idx,1);
%// Form ID array for X
lX = cellfun('length',X)
X_id = zeros(1,sum(lX))
X_id([1 cumsum(lX(1:end-1)) + 1]) = 1
X_id = cumsum(X_id)
Xr = cellfun(@(x) x(:).',X,'Uni',0); %//'# Convert to cells of row vectors
X1 = [Xr{:}] %// Get numeric array version
%// Detect cells that are to be processed by part1 (vectorized code)
[valid,idx1] = ismember(M(4,:),X1)
p1v = ~ismember(1:max(X_id),unique(X_id(accumarray(idx1(valid).',1)>1))) %//'
X_part1 = Xr(p1v)
X_part2 = Xr(~p1v)
%// Get decision masks from first and second passes and thus the final output
N = size(unqcols,2);
dm1 = first_pass(X_part1,M(4,:),subs_idx,counts,N)
dm2 = second_pass(X_part2,M(4,:),subs_idx,counts)
out = unqcols(:,~dm1 & ~dm2)
相关功能 -
function decision_mask = first_pass(X,M4,subs_idx,counts,N)
lensX = cellfun('length',X)'; %//'# Get X cells lengths
X1 = [X{:}]; %// Extract cell data from X
%// Finally, get the decision mask
vals = changem(X1,subs_idx,M4) .* ismember(X1,M4);
sums = cumsum(bsxfun(@eq,vals(:),1:N),1);
cumsums_at_shifts = sums(cumsum(lensX),:);
mask1 = any(bsxfun(@eq,diff(cumsums_at_shifts,[],1),counts(:).'),1); %//'
decision_mask = mask1 | cumsums_at_shifts(1,:) == counts(:).'; %//'
return
function decision_mask = second_pass(X,M4,subs_idx,counts)
%// Modify each cell of X based on their relevance with the fourth row of M
X1 = cellfun(@(x) subs_idx(ismember(M4,x)),X,'Uni',0);
lensX = cellfun('length',X1); %// Cell element count of X1
Xn = vertcat(X1{:}); %// Numeric array version of X
N = max(subs_idx); %// Number of unique subs_idx
%// Finally, get decision mask to select the correst columns from unqcols
sums = cumsum(bsxfun(@eq,Xn,1:N),1);
cumsums_at_shifts = sums(cumsum(lensX),:);
mask1 = any(bsxfun(@eq,diff(cumsums_at_shifts,[],1),counts(:).'),1); %//'
decision_mask = mask1 | cumsums_at_shifts(1,:) == counts(:).'; %//'
return
<强> Verficication 强>
本节列出了验证输出的代码。这是验证镜头#1代码的代码 -
%// Setup inputs and output
load('matrice_data.mat'); %// Load input data
X = cellfun(@(x) unique(x).',X,'Uni',0); %// Consider X's unique elements
out = shot1_func(M,X); %// output with Shot#1 function
%// Accumulate fourth row data from M based on the uniqueness from first 3 rows
[unqrows, ~, subs] = unique(M(1:3,:)','rows'); %//'
unqcols = unqrows.'; %//'
M4 = accumarray(subs(:),M(4,:).',[],@(x) {x}); %//'
M4 = cellfun(@(x) unique(x),M4,'Uni',0);
%// Find out cells in M4 that correspond to unique columns unqcols
[unqcols_idx,~] = find(pdist2(unqcols.',out.')==0);
%// Finally, verify output
for ii = 1:numel(unqcols_idx)
for jj = 1:numel(X)
if all(ismember(M4{unqcols_idx(ii)},X{jj}))
error('Error: Wrong output!')
end
end
end
disp('Success!')
答案 3 :(得分:1)
也许你可以使用2次cellfun
:
idxC = cellfun(@(a) ~any(cellfun(@(x) all(ismember(a,x)), X)), A4, 'un', 0);
idxC = cell2mat(idxC);
out = A(:,idxC)