Is there a way to vectorize the 3 for loops?

1 visualización (últimos 30 días)
Navneet
Navneet el 10 de Mzo. de 2013
for k=1:1:scolr
for j=1:1:scols
for i=1:1:srows
%Binary Conversion
esecretbin=dec2bin(secret(i,j,k),8);
esecreth(1:8)=str2num(esecretbin(:));
%Partial Share Generation
eoshare1{i,j}=esecreth;
%Dummy Initialization
cr=0;
zr=0;
%Random Integer Generation
ra{i,j,k}=randint(1,1,9);
while ra{i,j,k}==0
ra{i,j,k}=randint(1,1,9);
end
%Checking for 1s and 0s and replacing alternative 1s
for a=1:1:8
if eoshare1{i,j}(a)==1
o{i,j}=o{i,j}+1;
cr=cr+1;
if mod(o{i,j},2)==0;
eoshare1{i,j}(a)=0;
c{i,j}(a)=a;
insert1=1;
insert2=0;
else
c{i,j}(a)=0;
insert1=0;
insert2=1;
end
else
z{i,j}=z{i,j}+1;
c{i,j}(a)=0;
zr=zr+1;
end
if (cr<zr && insert1==0) || (cr<zr && insert1==1)
if c{i,j}(a)~=a
if eoshare1{i,j}(a)==0 && a~=ra{i,j,k}
eoshare1{i,j}(a)=1;
cr=cr+1;
zr=zr-1;
end
end
else if cr==zr && insert1==0
if c{i,j}(a)~=a
if eoshare1{i,j}(a)==0 && a~=ra{i,j,k}
eoshare1{i,j}(a)=1;
cr=cr+1;
zr=zr-1;
end
end
end
end
if cr>zr && insert1==0
break;
else if cr==zr && insert1==1
break;
end
end
end
%Performing bitxor to obtain Old Share2
eoshare2{i,j}=bitxor(eoshare1{i,j},esecreth);
%Insertion of the selected number at the random location in share1
enshare1{i,j}(1:ra{i,j,k}-1)=eoshare1{i,j}(1:ra{i,j,k}-1);
enshare1{i,j}(ra{i,j,k})=insert1;
enshare1{i,j}(ra{i,j,k}+1:9)=eoshare1{i,j}(ra{i,j,k}:8);
%Insertion of the selected number at the random location in share2
enshare2{i,j}(1:ra{i,j,k}-1)=eoshare2{i,j}(1:ra{i,j,k}-1);
enshare2{i,j}(ra{i,j,k})=insert2;
enshare2{i,j}(ra{i,j,k}+1:9)=eoshare2{i,j}(ra{i,j,k}:8);
%Cover image enhancement
if cover1(i,j,k)<256
cover1(i,j,k)=cover1(i,j,k)+1;
end
if cover2(i,j,k)<256
cover2(i,j,k)=cover2(i,j,k)+1;
end
isOne = enshare1{i,j} == 1 ;
enshare1{i,j}(:) = cover1(i,j,k)-1 ;
enshare1{i,j}(isOne) = cover1(i,j,k) ;
isOne = enshare2{i,j} == 1 ;
enshare2{i,j}(:) = cover2(i,j,k)-1 ;
enshare2{i,j}(isOne) = cover2(i,j,k) ;
%Reshaping into 3x3 matrix
etshare1{i,j}=reshape(enshare1{i,j},3,3);
eshare1{i,j}=transpose(etshare1{i,j});
etshare2{i,j}=reshape(enshare2{i,j},3,3);
eshare2{i,j}=transpose(etshare2{i,j});
%Dummy initialization
ro=(3*i)-2;
co=(3*j)-2;
%Replacement of cover pixels
s11=eshare1{i,j};
s1(ro:ro+2,co:co+2,k)=s11;
s22=eshare2{i,j};
s2(ro:ro+2,co:co+2,k)=s22;
end
end
end
Is there any way to vectorize the 3 for loops or atleast the inner 'a' for loop?
  6 comentarios
Navneet
Navneet el 10 de Mzo. de 2013
@Cedric
I saw your comment and I shall try that out. However, how do I vectorize the for loops of i,j and k? I understand the similarity between my previous question and the 'a' for loop over here. Let me try it out and get back.
With regards to the profile viewer, I tried it out. I did,
profile clear
profile on
profreport('filename.m')
However, it showed No profile information.
Cedric
Cedric el 10 de Mzo. de 2013
Editada: Cedric el 10 de Mzo. de 2013
>> profile viewer
and type the call of the function or script that you want to run/profile in the field Run this code. Then press enter or click on [ Start Profiling ].

Iniciar sesión para comentar.

Respuestas (2)

Daniel Shub
Daniel Shub el 10 de Mzo. de 2013
Why do you want to vectorize the code? Loops in MATLAB are no longer, and haven't been for a decade, inherently slow. Is your question perhaps "how can I optimize my code"? The first step is to use mlint. All those orange warning the MATLAB editor gives you are telling you important things. Once your function is all "green", it is time to use the profiler to find the bottleneck. Once you know what is slow, decide if it is worth speeding up.
  5 comentarios
Daniel Shub
Daniel Shub el 11 de Mzo. de 2013
Hopefully the checks are only a small portion of the run time and when they are not, hopefully there is already a compiled mex that runs without the checks....
Jan
Jan el 11 de Mzo. de 2013
@Daniel: Yes, hopefully. Unfortunately a lot of functions suffer from the overhead, e.g. polyfit: Disabling the warning temporarily needs more time than the actual processing, except for "large" problems.
In MEX files the checks require much less time, but they are much more important there. Inside C a bad input can cause a corrupted memory management, while inside Matlab this does not happen.

Iniciar sesión para comentar.


Jan
Jan el 10 de Mzo. de 2013
What kind of reply do you expect? I strongly recommend to use the profiler at first, or even better some TIC/TOCs, which do not disable the JIT acceleration. Then post the relevant part of the code again.
The currently posted large code is not a fair task for a forum.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by