1

Instead of concatening results to this, Is there other way to do the following, I mean the loop will persist but vector=[vector,sum(othervector)]; can be gotten in any other way?

vector=[]; 

while a - b ~= 0
  othervector = sum(something') %returns a vector  like [ 1 ; 3 ]
  vector=[vector,sum(othervector)]; 
  ...

end

vector=vector./100
cyborg
  • 9,989
  • 4
  • 38
  • 56
edgarmtze
  • 24,683
  • 80
  • 235
  • 386
  • 2
    What do `a` and `b` depend on? What does `something` depend on? On `a` and `b`? Is it always the same? – AVH Mar 31 '11 at 16:00
  • not really just to put a condition, what I would like to know is how to do `vector=[vector,sum(othervector)]; ` in other way, lets say the loop lasts 100 times – edgarmtze Mar 31 '11 at 16:14
  • Closely related/potential duplicate questions: [matlab - consider preallocating for speed](http://stackoverflow.com/questions/2151636/matlab-consider-preallocating-for-speed), [Growable data structure in MATLAB](http://stackoverflow.com/questions/2625048/growable-data-structure-in-matlab), [Matrix of unknown length in MATLAB?](http://stackoverflow.com/questions/1548116/matrix-of-unknown-length-in-matlab) – gnovice Mar 31 '11 at 17:45

2 Answers2

3

Well, this really depends on what you are trying to do. Starting from this code, you might need to think about the actions you are doing and if you can change that behavior. Since the snippet of code you present shows little dependencies (i.e. how are a, b, something and vector related), I think we can only present vague solutions.

I suspect you want to get rid of the code to circumvent the effect of constantly moving the array around by concatenating your new results into it.

First of all, just make sure that the slowest portion of your application is caused by this. Take a look at the Matlab profiler. If that portion of your code is not a major time hog, don't bother spending a lot of time on improving it (and just say to mlint to ignore that line of code).

If you can analyse your code enough to ensure that you have a constant number of iterations, you can preallocate your variables and prevent any performance penalty (i.e. write a for loop in the worst case, or better yet really vectorized code). Or if you can `factor out' some variables, this might help also (move any loop invariants outside of the loop). So that might look something like this:

vector = zeros(1,100);
while a - b ~= 0
  othervector = sum(something);
  vector(iIteration) = sum(othervector);
  iIteration = iIteration + 1;
end

If the nature of your code doesn't allow this (e.g. you are iterating to attain convergence; in that case, beware of checking equality of doubles: always include a tolerance), there are some tricks you can perform to improve performance, but most of them are just rules of thumb or trying to make the best of a bad situation. In this last case, you might add some maintenance code to get slightly better performance (but what you gain in time consumption, you lose in memory usage).

Let's say, you expect the code to run 100*n iterations most of the time, you might try to do something like this:

iIteration = 0;
expectedIterations = 100;

vector = [];
while a - b ~= 0
    if mod(iIteration,expectedIterations) == 0
      vector = [vector zeros(1,expectedIterations)];
    end
    iIteration = iIteration + 1;
    vector(iIteration) = sum(sum(something));
    ...

end

vector = vector(1:iIteration); % throw away uninitialized
vector = vector/100;

It might not look pretty, but instead of resizing the array every iteration, the array only gets resized every 100th iteration. I haven't run this piece of code, but I've used very similar code in a former project.

Egon
  • 4,757
  • 1
  • 23
  • 38
2

If you want to optimize for speed, you should preallocate the vector and have a counter for the index as @Egon answered already.

If you just want to have a different way of writing vector=[vector,sum(othervector)];, you could use vector(end + 1) = sum(othervector); instead.

groovingandi
  • 1,986
  • 14
  • 16