operation between timetables with different index
Afficher commentaires plus anciens
What i want to do is to subtract the first five elements of a timetable with just one another element of a different timetable with different indexes. The code below is kind of expensive as i want to apply this subtraction to a large dataset. Do you have any suggestion? thank you
T.Vull(1:5)-AvWe.Valp(1);
18 commentaires
What makes you think the above is "expensive"?
Other than there is no assignment of the results anywhere so all you'll get is the default ans variable for the result.
You could create a temporary of the constant, but that's done by the JIT optimizer behind the scenes anyway, and if you don't need that particular value for something else later on, there's no point in explicitly creating it.
Attilio Pittelli
le 6 Juil 2021
dpb
le 6 Juil 2021
Need more background on the use than this for it to make any sense at all...that's just a single operation but it has no point in isolation to do so if you don't do something with the result.
Attilio Pittelli
le 6 Juil 2021
Don't try to be so terse and assume we have any idea whatsoever of what you have -- we can't see your terminal and we don't know anything at all about what you're trying to do.
Explain the problem you're trying to solve and show us the data structure you're working with.
Nothing you've said so far indicates any sizable problem at all; if you have time-related data and need to process it in some manner, then using timetable and retime may be the trick...there are functions to get working weekdays, etc., etc., etc., and the isbetween facility to address subsections of timetables -- but we can't give advice of any depth unless we can understand the actual problem to be solved.
Or, rowfun and grouping variables are extremely powerful -- there are quite a number of my Answers here that use it to solve complicated problems quite easily -- although I've not thought to keep a link to stuff I've posted and the subject lines won't necessarily give any hint about rowfun being the silver bullet.
Attilio Pittelli
le 6 Juil 2021
Well, as said, that's a perfect application for either retime or rowfun, depending still on details you're holding too close to your chest for us to be able to do anything with.
But, I'm willing to bet it won't be much computationally once finally get to what the real problem actually is and the data table.
And it's a common misconception that it is necessary to create either new tables or files or otherwise physically separate out data by whatever grouping is desired in order to operate on the pieces independently. That is rarely actually needed to be done.
Attilio Pittelli
le 6 Juil 2021
Attilio Pittelli
le 6 Juil 2021
We can't debug what we can't see...attach a .mat file with the data table(s) and explain FULLY what the result wanted is.
"Not working" is a completely useless description -- we need the actual line of code and the complete error message in context to even have a chance -- and that would include every bit of the red text from one prompt to the next.
Being coy doesn't help; it just wastes both your time and ours...
The above is so; "help us help you!" by providing the data.
BUT, week() returns the yearly week number of the datetime vectors; it is NOT an indexing variable.
It could make a reasonable grouping variable, but
TT2 = retime(TT,'weekly',@yourFunctionHandle);
has a good chance of being able to be all the top level code you need once there's a definite description of what your function really needs to do.
Attilio Pittelli
le 6 Juil 2021
ONE LAST TIME!!!
Attach a sample of the data tables as .mat file(s) and give us a working definition of what the function is supposed to do. We can't code what we don't have a precise definition for.
I'm sure it can be done but not by a crystal ball...
It can take some ingenuity to write a function, but without a clear definition of PRECISELY what the inputs and expected output(s) are it's a fool's errand.
I'm willing to help (these challenges are kinda' fun and my entertainment), but can only work with what information am provided, and what has been given so far simply isn't sufficient to the task.
dpb
le 6 Juil 2021
If, indeed, there are data needed from two separate timetables, it may well behoove to synchronize them first.
But, again, we simply are too much in the dark as to what you're trying to do to know what the best approach would be...
Attilio Pittelli
le 7 Juil 2021
OK, that's a start; at least have data to work with -- now, what is the definition of the needed functionality in order to be able to know what to do next?
Remember, we know absolutely nothing but what you tell us -- only you know what you're trying to accomplish.
ADDENDUM:
However, I'd wonder if the real variable of interest for stock data would be the weekly average??? May be, I don't know, just seems like a peculiar choice to me, but again see the above...
Attilio Pittelli
le 7 Juil 2021
Well, maybe that's what the early request meant, but it wasn't easy to tell that... :) Language is undoubtedly part of the problem...but, that's a trivial operation although you need a grouping variable over week and year to do it. And, retime on its own isn't the tool; this is a job for rowfun -- but, to do it as neatly as would like, also takes an m-file function; not possible to rearrange the output as would like in an anonymous function.
It's still not too bad and is quite concise, but does, I'll grant, take some expertise in using MATLAB more than pure novice level... :)
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!