# How can I vectorise the next for - loop?

1 回表示 (過去 30 日間)
Raúl Alonso Merino 2019 年 1 月 16 日
Hello, I have the following for loop and I would like to know if there is a way to vectorise it:
JM = 91;
stepVec = 10;
JN = 100;
x = zeros(JM,JM);
y = zeros(JM,JM);
z = zeros(JM,JM);
v = zeros(1,3,JN);
kk=1;
for i=1:stepVec:JM
for ii=1:stepVec:JM
theta=ii*pi/180;
phi=i*pi/180;
x(i,ii) = r*sin(theta)*sin(phi);
y(i,ii) = r*sin(theta)*cos(phi);
z(i,ii) = r*cos(theta);
v(:,:,kk) = [x(i,ii) y(i,ii) z(i,ii)];
kk=kk+1;
end
end
Thank you for your answers, I would like to know if I can vectorise the next two too, although I don't think it's possible.
Rotat = zeros(4,4,length(t));
RotatVector = cell(1, JN);
F = struct('cdata',cell(1,length(t)),'colormap',cell(1,length(t)));
Faxis = cell(1,JN);
for j=1:JN
for i=1:length(t)
RR = makehgtform('axisrotate',[v(1,1,j) v(1,2,j) v(1,3,j)],i*beta);
set(hgtc,'Matrix',RR);
Rotat(:,:,i) = RR;
RotatVector{1,j} = Rotat;
width = 300;
height = 225;
F(i) = getframe(gcf,[140 109.5 width height]);
Faxis{j} = F;
drawnow;
end
end
% --------------------------------------------------------------------------
grayFrame = zeros(282,375,length(F));
grayFrameNEW = cell(1,JN);
meanGrayLevels = cell (1,length(F));
meanGrayLevelsCELL = cell(1,length(F),JN);
for j=1:JN
for i=1:length(F)
% convert the image to a frame
grayFrame(:,:,i) = rgb2gray(Faxis{j}(i).cdata);
grayFrameNEW{j}=grayFrame;
% Calculate the mean gray level
meanGrayLevels{i} = mean2(grayFrameNEW{1,j}(:,:,i));
meanGrayLevelsCELL(:,:,j) = meanGrayLevels;
end
end
##### 2 件のコメントなしを表示なしを非表示
Walter Roberson 2019 年 1 月 16 日
You cannot vectorize getframe(), so you will not be able to avoid looping for that.
Faxis{j} = F;
should be after the "for i" loop.
Raúl Alonso Merino 2019 年 1 月 17 日
Thank you, I have corrected that, about the third loop is there something possible?

サインインしてコメントする。

### 採用された回答

Walter Roberson 2019 年 1 月 16 日
for i=1:stepVec:JM
for ii=1:stepVec:JM
theta=ii*pi/180;
phi=i*pi/180;
x(i,ii) = r*sin(theta)*sin(phi);
y(i,ii) = r*sin(theta)*cos(phi);
z(i,ii) = r*cos(theta);
v(:,:,kk) = [x(i,ii) y(i,ii) z(i,ii)];
kk=kk+1;
end
end
can be replaced with
angles = (1:stepVec:JM) * pi/180;
sinang = sin(angles);
cosang = cos(angles);
nang = length(angles);
rsin = r * sinang;
rcos = r * cosang;
k = 1;
for i = 1 : nang
x(i, :) = rsin(i) * sinang;
y(i, :) = rsin(i) * cosang;
z(i, :) = rcos;
v(1, :, k:k+nang-1) = [x y z];
k = k + nang;
end
and this can be further vectorized:
angles = (1:stepVec:JM) * pi/180;
nang = length(angles);
[sTheta, sPhi] = ndgrid(sin(angles));
[cTheta, cPhi] = ndgrid(cos(angles));
v = r .* [sTheta(:) .* sPhi(:), sTheta(:) .* cPhi(:), cTheta(:)]; %(nang^2) x 3 x 1
v = permute(v, [3 1 2]); %1 x 3 x (nang^2)
##### 7 件のコメント5 件の古いコメントを表示5 件の古いコメントを非表示
Walter Roberson 2019 年 1 月 18 日
You can initialize
meanGrayLevels = zeros(length(t), JN);
Then as you capture a frame into F (probably without index),
meanGrayLevels(i, j) = mean2( rgb2gray(F.cdata) );
Raúl Alonso Merino 2019 年 1 月 22 日
Thanks that worked perfectly, it took it 54 hours to compute but it was because I choose a JN = 129600 as I have two angles of 360 degrees each and I wanted to rotate for all the degrees. I will have to think now how to reduce time on it but thanks.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

R2018b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by