How can I vectorise the next for - loop?

조회 수: 3 (최근 30일)
Raúl Alonso Merino
Raúl Alonso Merino 2019년 1월 16일
댓글: Raúl Alonso Merino 2019년 1월 22일
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
Walter Roberson 2019년 1월 16일
You cannot vectorize getframe(), so you will not be able to avoid looping for that.
However, your line
Faxis{j} = F;
should be after the "for i" loop.
Raúl Alonso Merino
Raúl Alonso Merino 2019년 1월 17일
Thank you, I have corrected that, about the third loop is there something possible?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
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
Walter Roberson
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
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.
I'll give your answer as solution as you were really helpful. Thank you a lot.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by