From: Walter Roberson on 8 Jun 2010 16:05 Wayne King wrote: > Hi Marios, If you want the Attributes field to show up as complex, you > can do > > x = complex(zeros(5,6), zeros(5,6)); Small note: The above can be abbreviated to x = complex(zeros(5,6)) However, x = repmat(complex(0),5,6) will demote the complex 0 to double and replicate that.
From: Walter Roberson on 8 Jun 2010 17:13 Bruno Luong wrote: > There is no need to preallocate the imaginary part: as soon as the first > true complex element is assigned to your array, the whole memory block > (same size as the real part) reserved for imaginary part will be > allocated. All you need is allocated with zeros(), don't bother with the > rest. There are cases where you might want to bother: if you are tight on memory, then you want to be allocating memory under controlled conditions, perhaps even determining the maximum size of some array (if only a work array) by trying sizes until the size is accepted. In such a case you would want the entire memory for the complex array (which you know you will be using) to be allocated under the controlled conditions, rather than risking that that much memory might not be available when it is needed.
From: James Tursa on 8 Jun 2010 17:17 Marios Karaoulis <marios.karaoulis(a)gmail.com> wrote in message <fa14bad0-db5f-4f9d-be39-0e46eff3288f(a)31g2000prc.googlegroups.com>... > I think, that matlab automatically checks if there is reason for a > matrix to be complex or not (so reduce the memory size). > This way it is slower if you preallocate a complex matrix as real, and > then during execution you change an element so it can be compelx. Then > matlab re-preallocate another matrix so it can handle complex numbers. The overall execution is not any faster just because you pre-allocate the imaginary part. You have simply shifted the one-time initial work of allocating the imaginary part from one place in your code to another. Like others have already stated, in most cases it is usually pointless to explicitly pre-allocate the imaginary part since it will happen automatically in the background when needed. The one area that it *will* make a difference is if you are using embedded MATLAB and need a true complex variable to pass as an argument to a function that requires the same. (Another reason might be a mex routine that operates in-place on an array and returns a complex result, but that is beyond the scope of this thread). James Tursa
From: Bruno Luong on 8 Jun 2010 18:21 Walter Roberson <roberson(a)hushmail.com> wrote in message <humbtg$c2o$3(a)canopus.cc.umanitoba.ca>... > > There are cases where you might want to bother: if you are tight on memory, > then you want to be allocating memory under controlled conditions, perhaps > even determining the maximum size of some array (if only a work array) by > trying sizes until the size is accepted. In such a case you would want the > entire memory for the complex array (which you know you will be using) to be > allocated under the controlled conditions, rather than risking that that much > memory might not be available when it is needed. First if you have that much memory, then you are already in trouble. Second it is not easy control, after all we are using a comfortable I-take-care-all Matlab. Example: >> x=complex(zeros(3)) x = 0 0 0 0 0 0 0 0 0 >> isreal(x) ans = 0 >> x(1)=0 x = 0 0 0 0 0 0 0 0 0 >> isreal(x) ans = 1 Now a more interesting question IMO is: does Matlab need to run over the entire imaginary array everytime to check for zero whenever an element is modified? It seems like a very inefficient strategy. How to disable this "feature" (IMO it's not possible)? Unrelated topic, but a wish from me is the possibility for users to force *integer* index output for command such as FIND, SORT, UNIQUE, ISMEMBER, HISTC, etc... Bruno
From: James Tursa on 8 Jun 2010 18:51 "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <humfoi$oeg$1(a)fred.mathworks.com>... > > Now a more interesting question IMO is: does Matlab need to run over the entire imaginary array everytime to check for zero whenever an element is modified? It seems like a very inefficient strategy. How to disable this "feature" (IMO it's not possible)? There is a status word in the mxArray header stuff. e.g., they have a status bit for "real double scalar", etc. But I am unaware of a status bit for "imaginary part is presently all zero" that could be exploited. I will check ... James Tursa
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: .shp and .fig on same Figure Next: fixing a number in a matrix |