Which is faster, a row vector or a column vector? Can anyone answer me please?

I am trying to do some integration so I am defining the output as a matrix, my question is which definition will give me faster results to define my matrix :
A = zeros(1,n) or A = zeros(n,1)?
Regards

 Akzeptierte Antwort

Stephen23
Stephen23 am 13 Mai 2015
Bearbeitet: Stephen23 am 15 Mai 2015
Perhaps the column, as MATLAB uses column-major memory storage of arrays. But really it is unlikely to make a big difference.
Just try it and use timeit or tic and toc to compare the times.
Another very fast way to define a matrix of zeros is this:
A(n) = 0;
Or if speed really is that critical, you might like to check out some of the submissions on MATLAB File Exchange:

7 Kommentare

Actually, My time vector is (0 : 10 : 1000) and I am doing an integration for (0 to 10) then from 0 to 40 then from 0 to 60 and so on ... I am changing two parameters values 16 times so my code is taking 3 days to give me the results. What I am looking for is getting the results in shorter time(may be one day or less), at least I hope so.
Regards
Stephen23
Stephen23 am 13 Mai 2015
Bearbeitet: Stephen23 am 13 Mai 2015
The bottle-neck is unlikely to be the generation of some vector, but is much more likely to be the algorithm itself. There are specific ways to writing fast and efficient code, and of course these are different for every programming language. For MATLAB you will find lots of advice on this forum and also in the documentation:
If you upload the complete code in a comment, then we can try it out, check the algorithm and tell you if there are any parts that could be made faster. To upload your file click the paperclip button above the textbox, and then press both buttons: Choose file and then Attach file.
This is only fixed values for my two parameters (r,s)so if I need to vary them 16 times, it is taking days to give me results.
Your first output jt(1) is always going to be infinity because you divide your integral by t(1,i) and your t(1,1) is 0.
For the case of the r, s combination you show in that file, there is a closed form integral. But it involves large numbers. When you evaluate in double precision, you will run into notable difficulties in round-off. There is also a lot of sin(r/s * t) and cos(r/s*t) implying that the values change a fair bit (r/s/Pi) (=5/2/Pi = about 0.79) cycles per integer t, but you are evaluating t at intervals of 10, so your plot might be more or less plotting accidents of phase instead of plotting meaningful trends.
I understood about jt(1) but I could not understand the rest of your explanation, I have sin or cos of ( r*t) not sin or cos(r/s *t) unless you supposed something else. Can you explain more but in simple words and how to sort those issues please? What are your suggestions?
Regards
My checking shows that if you use 5 for r, and leave s symbolic, then there is a closed-form integral for the expression -- something that could be evaluated once per t rather than having to do the time-consuming numeric integration.
In the case of r held at 5, the integral for turns complex if s becomes larger than about 10.3 or if s is negative (but might become positive again below -10.3).
When you are varying r and s for your 16 runs, what are the values you are testing with?
r = 1,2.5,5,10
s = 1,3,5,10

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 13 Mai 2015
For any of the arithmetic operations, a row vector is exactly the same speed as a column vector. However, extracting a column vector from a matrix is faster than extracting a row vector. Also, library functions often are defined as returning column vectors and their logic is sometimes marginally faster on column vectors.

Kategorien

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by