I want to run my script more speed. It spent two hours
    1 Ansicht (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
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 Kommentare
  dpb
      
      
 am 15 Feb. 2025
				
      Bearbeitet: dpb
      
      
 am 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
...
Antworten (0)
Siehe auch
Kategorien
				Mehr zu Logical finden Sie in Help Center und File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




