Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble in Matlab Preallocating with a Complex For Loop
    primarykey
    data
    text
    <p>I am fairly new to matlab but for my job I need to import an ENORMOUS data set and organize it in a certain way. I have written a code that will do this, but very ineffieciently (it is only my third major piece of code and it takes several hours). Matlab is telling me that I can preallocate my variables (about fifty times in fact) but I am having trouble seeing how to do that because I am not sure what matrix the data will be added to for each iteration in the for loop. The code itself probably explains this better than I do.<br> (This is just a small piece of it, but will hopefully display my problem)</p> <pre><code>for x= 1:length(firstinSeq) for y= 1:length(littledataPassed-1) if firstinSeq(x,1)== littledataPassed(y,1) &amp;&amp; firstinSeq(x,2)== littledataPassed(y,2) switch firstinSeq(x,3) case 0 for z= 0:1000 w= y+z; if firstinSeq(x,4)== littledataPassed(w,4) if littledataPassed(w,6)== 1 &amp;&amp; firstinSeq(x,2)== littledataPassed(w,2) &amp;&amp; littledataPassed(w,5)== 0 msgLength0= [msgLength0; firstinSeq(x,:) littledataPassed(w,:)]; break else continue end else msgLength0= [msgLength0; firstinSeq(x,:) [0 0 0 0 0 0]]; break end end case 1 for z= 0:1000 w= y+z; if firstinSeq(x,4)== littledataPassed(w,4) %if sequence not the same, terminate if littledataPassed(w,6)== 1 &amp;&amp; firstinSeq(x,2)== littledataPassed(w,2) &amp;&amp; littledataPassed(w,5)== 0 msgLength1= [msgLength1; firstinSeq(x,:) littledataPassed(w,:)]; break else continue end else msgLength1= [msgLength1; firstinSeq(x,:) [0 0 1 0 0 0]]; break end end case 2 for z= 0:1000 w= y+z; if firstinSeq(x,4)== littledataPassed(w,4) if littledataPassed(w,6)== 1 &amp;&amp; firstinSeq(x,2)== littledataPassed(w,2) &amp;&amp; littledataPassed(w,5)== 0 msgLength2= [msgLength2; firstinSeq(x,:) littledataPassed(w,:)]; break else continue end else msgLength2= [msgLength2; firstinSeq(x,:) [0 0 2 0 0 0]]; break end end for z= 0:1000 w= y+z; if firstinSeq(x,4)== littledataPassed(w,4) if littledataPassed(w,6)== 1 &amp;&amp; firstinSeq(x,2)== littledataPassed(w,2) &amp;&amp; littledataPassed(w,5)== 1 msgLength2= [msgLength2; firstinSeq(x,:) littledataPassed(w,:)]; break else continue end else msgLength2= [msgLength2; firstinSeq(x,:) [0 0 2 0 1 0]]; break end end </code></pre> <p>any thoughts on how I could preallocate these variables(msgLength0,1,2,etc)? They do not have data added for every value in the loop and I am uncertain of the end size for each run. There are a total of eight cases for my switch right now, making this program very slow.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload