prova.mat在MATLAB中考虑以下列方式获得
for w=1:100
for p=1:9
A{p}=randn(100,1);
end
baseA_.A=A;
eval(['baseA.A' num2str(w) '= baseA_;'])
end
save(sprintf('prova.mat'),'-v7.3', 'baseA')
Run Code Online (Sandbox Code Playgroud)
为了了解我的数据中的实际维度,1x9 cellin A1由以下9数组组成:904x5, 913x5, 1722x5, 4136x5, 9180x5, 3174x5, 5970x5, 4455x5, 340068x5.另一个Aj有类似的构成.
请考虑以下代码
clear all
load prova
tic
parfor w=1:100
indA=sprintf('A%d', w);
Aarr=baseA.(indA).A;
Boot=[];
for p=1:9
C=randn(100,1).*Aarr{p};
Boot=[Boot; C];
end
D{w}=Boot;
end
toc
Run Code Online (Sandbox Code Playgroud)
如果我在我的Macbook Pro中parfor使用4本地工作人员运行循环,则需要1.2秒.parfor用for它替换需要0.01秒.
根据我的实际数据,时间差为31秒对7秒[矩阵的创建C也更复杂].
如果已正确理解问题是计算机必须发送baseA给每个本地工作人员,这需要时间和内存.
您能否提出一个能够parfor比方便更方便的解决方案for?我认为保存所有单元格baseA是一种通过在开始时加载一次来节省时间的方法,但也许我错了.
我对MATLAB(slicesample)中的算法切片采样有疑问.
考虑一个随机变量,g(X)使用未标准化的pdf f:=1(g(X)=g),其中g是一个实数.
假设我计算g(X)的辅助功能,@dens_slice对于任何给定值x的X.
我们starting是一个值x的X这样g(X)=g.
然后我就设定了
slicesample(starting,num,'pdf',@dens_slice);
Run Code Online (Sandbox Code Playgroud)
与num=2000表示该输入nsamples.
假设我将g(X)算法所做的所有评估保存在列向量中A.一段时间后,算法停止A并由多于2000值组成.
是否2000对应于A等于的值的数量g?
例:
%%%%%%MAIN%%%%%%%
clear all
%Set slice sampling
rng('default');
rng(1);
global U
U = randn(2, 1);
S_init = U(1)*3 + U(2)*2;
global starting
starting = [S_init …Run Code Online (Sandbox Code Playgroud) 我有A维度的单元格数组m * k.
我想保持A唯一的行数达到k个单元格的顺序.
的"棘手"的部分是"高达K个小区的顺序":考虑k了在细胞中i的第i行A,A(i,:); 有可能是一个连续j的A,A(j,:)即相当于A(i,:)达到它的重新排序k的细胞,这意味着,例如,如果k=4它可能是:
A{i,1}=A{j,2}
A{i,2}=A{j,3}
A{i,3}=A{j,1}
A{i,4}=A{j,4}
Run Code Online (Sandbox Code Playgroud)
我现在正在做的是:
G=[0 -1 1; 0 -1 2; 0 -1 3; 0 -1 4; 0 -1 5; 1 -1 6; 1 0 6; 1 1 6; 2 -1 6; 2 0 6; 2 1 6; 3 -1 6; 3 0 6; …Run Code Online (Sandbox Code Playgroud) 假设我在MATLAB中有一个A维度矩阵Nx(N-1),例如
N=5;
A=[1 2 3 4;
5 6 7 8;
9 10 11 12;
13 14 15 16;
17 18 19 20 ];
Run Code Online (Sandbox Code Playgroud)
我想通过添加零对角线转换A成NxN矩阵B,即
B=[ 0 1 2 3 4;
5 0 6 7 8;
9 10 0 11 12;
13 14 15 0 16;
17 18 19 20 0];
Run Code Online (Sandbox Code Playgroud)
这段代码做我想要的:
B_temp = zeros(N,N);
B_temp(1,:) = [0 A(1,:)];
B_temp(N,:) = [A(N,:) 0];
for j=2:N-1
B_temp(j,:)= [A(j,1:j-1) 0 A(j,j:end)];
end
B …Run Code Online (Sandbox Code Playgroud) 我有矩阵A在尺寸Matlab的hxk和矩阵B尺寸yxk.我想在每一行中构建一个C维度yx1列表向量,j其中B(j,:)出现了多少次A.
我有一个线性不等式/等式系统在Matlab中解决我使用linprog.由于一些不等式是严格的,我使用一个非常小的常数eps来得到严格的包含,如这里所解释的
solve下面的函数在提供了一个值后解决了系统问题eps.
function pj=solve(eps)
%Inequalities
%x(1)-x(5)-x(9)-x(10)-x(11)-x(15)-x(16)-x(17)-x(19)<=0;
%x(2)-x(6)-x(9)-x(12)-x(13)-x(15)-x(17)-x(18)-x(19)<=0;
%x(3)-x(7)-x(10)-x(12)-x(14)-x(16)-x(17)-x(18)-x(19)<=0;
%x(4)-x(8)-x(11)-x(13)-x(14)-x(15)-x(16)-x(18)-x(19)<=0;
%x(1)+x(2)-x(5)-x(9)-x(10)-x(11)-x(15)-x(16)-x(17)-x(19)-...
% x(6)-x(12)-x(13)-x(18)<=0;
%x(1)+x(3)-x(5)-x(9)-x(10)-x(11)-x(15)-x(16)-x(17)-x(19)-...
% x(7)-x(12)-x(14)-x(18)<=0;
%x(1)+x(4)-x(5)-x(9)-x(10)-x(11)-x(15)-x(16)-x(17)-x(19)-...
% x(8)-x(13)-x(14)-x(18)<=0;
%x(2)+x(3)-x(6)-x(9)-x(12)-x(13)-x(15)-x(17)-x(18)-x(19)-...
% x(7)-x(10)-x(14)-x(16)<=0;
%x(2)+x(4)-x(6)-x(9)-x(12)-x(13)-x(15)-x(17)-x(18)-x(19)-...
% x(8)-x(11)-x(14)-x(16)<=0;
%x(3)+x(4)-x(7)-x(10)-x(12)-x(14)-x(16)-x(17)-x(18)-x(19)-...
% x(8)-x(11)-x(13)-x(15)<=0;
%x(1)+x(2)+x(3)-x(5)-x(9)-x(10)-x(11)-x(15)-x(16)-x(17)-x(19)-...
% x(6)-x(12)-x(13)-x(18)-...
% x(7)-x(14)<=0;
%x(1)+x(2)+x(4)-x(5)-x(9)-x(10)-x(11)-x(15)-x(16)-x(17)-x(19)-...
% x(6)-x(12)-x(13)-x(18)-...
% x(8)-x(14)<=0;
%x(1)+x(3)+x(4)-x(5)-x(9)-x(10)-x(11)-x(15)-x(16)-x(17)-x(19)-...
% x(7)-x(12)-x(14)-x(18)-...
% x(8)-x(13)<=0;
%x(2)+x(3)+x(4)-x(6)-x(9)-x(12)-x(13)-x(15)-x(17)-x(18)-x(19)-...
% x(7)-x(10)-x(14)-x(16)-...
% x(8)-x(11)<=0;
%Equalities
%x(1)+x(2)+x(3)+x(4)=1;
%x(5)+x(6)+x(7)+x(8)+x(9)+x(10)+x(11)+x(12)+x(13)+x(14)+x(15)+x(16)+x(17)+x(18)+x(19)=1;
%I also want each component of x to be different from 1 and 0 (strictly included between 1 and 0 given …Run Code Online (Sandbox Code Playgroud) 考虑以下对2x1Matlab中矢量的绘制,其概率分布是两个高斯分量的混合.
P=10^3; %number draws
v=1;
%First component
mu_a = [0,0.5];
sigma_a = [v,0;0,v];
%Second component
mu_b = [0,8.2];
sigma_b = [v,0;0,v];
%Combine
MU = [mu_a;mu_b];
SIGMA = cat(3,sigma_a,sigma_b);
w = ones(1,2)/2; %equal weight 0.5
obj = gmdistribution(MU,SIGMA,w);
%Draws
RV_temp = random(obj,P);%Px2
% Transform each component of RV_temp into a uniform in [0,1] by estimating the cdf.
RV1=ksdensity(RV_temp(:,1), RV_temp(:,1),'function', 'cdf');
RV2=ksdensity(RV_temp(:,2), RV_temp(:,2),'function', 'cdf');
Run Code Online (Sandbox Code Playgroud)
现在,如果我们检查是否RV1并且通过这样做RV2均匀分布[0,1]
ecdf(RV1)
ecdf(RV2)
Run Code Online (Sandbox Code Playgroud)
我们可以看到它RV1是均匀分布的[0,1](经验cdf接近45度线)而RV2不是.
我不明白为什么.似乎距离越远, …
我在Matlab向量B维nx1包含整数从1到n按照一定的顺序,例如n=6 B=(2;4;5;1;6;3).
我有一个矢量A维数mx1与m>1包含在升序每一个重复任意次数,例如,相同的整数m=13 A=(1;1;1;2;3;3;3;4;5;5;5;5;6).
我想获得C维度mx1,其中整数in A按顺序重新排序B.在这个例子中,C=(2;4;5;5;5;5;1;1;1;6;3;3;3)
我有一个矩阵index在Matlab与尺寸GxN和矩阵A与尺寸MxN.
在提出我的问题之前,让我举一个例子.
clear
N=3;
G=2;
M=5;
index=[1 2 3;
13 14 15]; %GxN
A=[1 2 3;
5 6 7;
21 22 23;
1 2 3;
13 14 15]; %MxN
Run Code Online (Sandbox Code Playgroud)
我想您的帮助,以构建一个矩阵Response,大小GxM与Response(g,m)=1行是否A(m,:)等于index(g,:)否则为零.
继续上面的例子
Response= [1 0 0 1 0;
0 0 0 0 1]; %GxM
Run Code Online (Sandbox Code Playgroud)
这段代码做了我想要的(取自我之前的一个问题 - 只是为了澄清:当前的问题是不同的)
Response=permute(any(all(bsxfun(@eq, reshape(index.', N, [], G), permute(A, [2 3 4 1])), 1), 2), [3 4 1 …Run Code Online (Sandbox Code Playgroud) 我A在维度的Matlab中有一个矩阵MXN.我想创建一个B维度向量,Mx1其中B(i)=1if A(i,:)不会多次重复A,0否则.
例如
A=[1 2 3;
4 5 6;
1 2 3;
7 8 9];
B=[0;1;0;1];
Run Code Online (Sandbox Code Playgroud)
这段代码
[vu,vx,vx]=unique(A,'rows');
n=accumarray(vx,1);
C=[vu n]
Run Code Online (Sandbox Code Playgroud)
有助于找到每行的出现次数.因此,通过添加一个循环,我应该能够C得到B所需的.但是,在我的实际情况下M非常大(80000).我能用得更快吗?
matlab ×10
matrix ×3
performance ×2
arrays ×1
cell-array ×1
comparison ×1
optimization ×1
parfor ×1
permutation ×1
random ×1
rows ×1