I want to run my script more speed. It spent two hours

1 view (last 30 days)
ar = ['a','a','a','a','a','a'] % this matrix have 3920 rows
r = [1 2 3 4 6 7 8 11 14 17 19 22 23 24 25] %this matrix have 162 rows
w = [1 2 3 4 5 6 7 10 11 14 17 18 22 23 24 1 2 3 4 5 7 8 9 10 11 15 16 17 23 24 2 3 4 9 10 11 12 13 16 17 18 19 20 23 25 2 6 7 8 9 10 12 14 16 17 19 20 22 23 25] %this matrix have 10000 rows
x1 = 1;
arlin1 = 1;
arcol1 = 1;
rlin1 = 1;
wlin1 = 1;
nlin1 = 1;
ncol1 = 1;
ncol2 = 3921;
v = 0;
v1 = 1;
ctr1 = 1;
for i3 = 1:10000
for i2= 1:3920
for i1 = 1:27
while x1 < 7
if ar(arlin1, arcol1) == "a"
d = ismember(r(rlin1,:), w(wlin1,1:15));
sum(d);
if sum(d) > 10
v = v + 1;
break
end
elseif ar(arlin1, arcol1) == "b"
d = ismember(r(rlin1,:), w(wlin1,16:30));
sum(d);
if sum(d) > 10
v = v + 1;
break
end
elseif ar(arlin1, arcol1) == "c"
d = ismember(r(rlin1,:), w(wlin1,31:45));
sum(d);
if sum(d) > 10
v = v + 1;
break
end
elseif ar(arlin1, arcol1) == "d"
d = ismember(r(rlin1,:), w(wlin1,46:60));
sum(d);
if sum(d) > 10
v = v + 1;
break
end
end
arcol1 = arcol1 + 1;
rlin1 = rlin1 + 1;
x1 = x1 + 1;
end
x1 = 1;
arcol1 = 1;
ctr1 = ctr1 + 6;
rlin1 = ctr1;
end
n(nlin1,ncol1) = v;
v = 0;
ncol1 = ncol1 + 1;
arlin1 = arlin1 + 1;
ctr1 = 1;
rlin1 = ctr1;
end
arlin1 = 1;
wlin1 = wlin1 + 1;
nlin1 = nlin1 + 1;
ncol1 = 1;
end
nlin1 = 1;
while v1 < 10001
y = max(n(nlin1,:));
n(nlin1, ncol2) = y;
nlin1 = nlin1 + 1;
v1 = v1 + 1;
end
  10 Comments
AIRTON
AIRTON on 15 Feb 2025
Edited: dpb on 15 Feb 2025
Hi Walter,
Greets
As I said, my code have slowed two hours, but
with yours notes I have perfected it and now it runs at one hour and 17 minutes.
I thank you Very much!
Please, see like it is.
ar = 3920 arrangements
r = 162 combinations
w = 10000 combinations
n = zeros(10000,3921);
arlin1 = 1;
arcol1 = 1;
rlin1 = 1;
wlin1 = 1;
nlin1 = 1;
ncol1 = 1;
ncol2 = 3921;
v = 0;
ctr1 = 1;
for i3 = 1:10000
for i2= 1:3920
for i1 = 1:27
for i0 = 1:6
if ar(arlin1, arcol1) == 1
d = ismembc(r(rlin1,:), w(wlin1,1:15));
elseif ar(arlin1, arcol1) == 2
d = ismembc(r(rlin1,:), w(wlin1,16:30));
elseif ar(arlin1, arcol1) == 3
d = ismembc(r(rlin1,:), w(wlin1,31:45));
elseif ar(arlin1, arcol1) == 4
d = ismembc(r(rlin1,:), w(wlin1,46:60));
end
sum(d);
if sum(d) > 10
v = v + 1;
break
end
arcol1 = arcol1 + 1;
rlin1 = rlin1 + 1;
end
arcol1 = 1;
ctr1 = ctr1 + 6;
rlin1 = ctr1;
end
n(nlin1,ncol1) = v;
v = 0;
ncol1 = ncol1 + 1;
arlin1 = arlin1 + 1;
ctr1 = 1;
rlin1 = ctr1;
end
arlin1 = 1;
wlin1 = wlin1 + 1;
nlin1 = nlin1 + 1;
ncol1 = 1;
end
y=max(n,[],2);
Let me tell him my two principal goals:
  • To come to the values of “y” (last row of the code – “y=max(n,[],2);”)
  • To run it in the Lesser time possible.
I attach three zip files.
If can you help me?
I thank you Very much.
dpb
dpb on 15 Feb 2025
Edited: dpb on 15 Feb 2025
Well, we understand the end result is the y array; the problem is we don't know what is the underlying problem definition/description other than trying to reverse engineer your code. That is a nonproductive way to approach the problem.
What is needed is the problem to be solved verbally described as basic functional requirements. From such a description one can devise an algorithm to solve the problem as did your brute-force iterative solution above; the issue is that as you've seen, trying to optimize code may produce somewhat faster results but real improvements in speed (like order of magnitude) will have to come from better algorithms. Better algorithms will only come from having a statement of the problem.
That said, your code above still has the totally superfuous sum(d); line which is doing nothing but wasting time, particularly since you're doing it twice...
...
d = ismembc(r(rlin1,:), w(wlin1,46:60));
end
sum(d);
if sum(d) > 10
v = v + 1;
break
end
should be
...
d = ismembc(r(rlin1,:), w(wlin1,46:60));
end
%sum(d); % wasted operation; perhaps useful for debugging, but a total waste in production code
if nnz(d)>10 % just count logicals, no need to add...
v = v + 1;
break
end
...

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!