Issue transposing a matrix inside of a timeseries

Hello everyone,
Im trying to transpose a matrix defined inside of a timeseries:
Rk = timeseries(parentDataset.reflectometry.density', parentDataset.reflectometry.time);
parentDataset.reflectometry.density is a 7x1 matrix that I need to transpose to become 1x7 however its returning a 1x1x7. I tried using squeeze, reshape or permute to no avail.
I also tried transposing the matrix before feeding it inside the timeseries but I'm also meeting the same issue.
What really confuses me is that when parentDataset.reflectometry.density is a 1x7, transposing it actually returns a 7x1 matrix (and returns 2 when checking wiht ndims).
How can i fix this? And what am I misunderstnading?
Thanks for any help!

 Accepted Answer

dpb
dpb on 23 Jul 2025
Edited: dpb on 23 Jul 2025
This is unavoidable with the timeseries object given its internal design and restrictions on dimensions compatibilities. See the description of the input parameters for Data and Time Values. The timeseries times are always a column vector and "Either the first or the last dimension of the data must align with the orientation of the time vector."
Why, specifically, the implementation chooses to use the 3D array by plane here instead of just ignoring the row vector and forcing the column vector for the data series as it does for the time data I don't know, but that's how it's implemented and is nothing can be done about it (other than not trying to do the impossible, anyway).
The following is the constructor for the timeseries object that results in the given orientation if the input data is a row vector --
...
size_data = size(data);
if length(size_data)==2 && size_data(end)==len && size_data(1)~=len && len > 1
data = reshape(data,[size_data(1:end-1) 1 len]);
end
If size_data is [1,7] as in your example, then size_data(1:end-1) --> |size_data(1:2-1) --> size_data(1) == 1 and the resulting expression is
data = reshape(data,1 1 7);
Why do you think you need to transpose the data array, anyway? Each observation should go with each time value; if you're trying to create a multiple-variable timeseries, then it would need to be 2D array of however many variables by the length of the time vector.
Personally, I've found the timeseries to be more trouble than its worth with the peculiarities of its interface and usage; unless you've found the one particular usage that does happen to fit, I'd suggest using the timetable object instead.

11 Comments

Andrew
Andrew on 23 Jul 2025
Edited: Andrew on 23 Jul 2025
Hi,
Unfortunately, I have to transpose the matrix as it is ported to a simulink model and for some reason if i keep it as a 1x1x7 my inport has 1x1 dimensions instead of 1x7.
Also, I have a Dimensions class used in other parts of the program that requires it to be a double vector aka a 2D space.
I will try and look at timetables and how they would interact with the rest of the program and the simulink model.
Thanks for the help!! Realy appreciate it
dpb
dpb on 23 Jul 2025
Edited: dpb on 23 Jul 2025
I "know nuthink!" a la Sgt Schultz about Simulink, so not much help there.
Can you replace the Simulink import with an S-function that uses the timeseries and returns the desired vectors? Then you wouldn't have to try to force a row vector into the timeseries but could then transpose the resulting .Data column vector to a row vector.
Or, can you not just use the vectors themselves without the timeseries object at all?
yeah well it is my first time using simulink so its a learn as I go process and it has been less than a week since im really at it so im sorry i wasnt much help.
To answer your questions, I havent tried using an S-function because well, im not familiar with what that is, will look into it.
And no i do need the vectors to be inside a timeseries unfortunately, when I did try to use temporary vectors not imbedded in a timeseries my simulink model didnt compile correctly, and upon contacting the first developer of the program he did confirm the need of a timeseries object.
Again, sorry if im not of much help im very much a beginner at simulink models, so if you could excuse me.
Well, you have a week on me; I've never even seen a Simulink installation, so I'm of no help at all, sorry.
All I know about S functions is that they give you an interfacee between Simulink and MATLAB to let you write MATLAB code and pass data to/from Simulink that you can manipulate in MATLAB. You won't be able to turn the timeseries internal storage around, but you could manipulate the timeseries object and get the data out as row vectors as 2D objects.
This raises another Q? though that if this is from another source, why did not the original developer have the same problem? Are you trying to do something that it wasn't designed to have done with it, perhaps, is the problem?
No worries, we tried our best.
The original developer was working with another measurement method which yielded [10 x 77000] matrices and when he transposed them he got [77000 x 10] matrices with no issues. I think having my second dimension = 1 is causing issues because when i tried to transpose a 1x7 matrix i get a 7x1 matrix with no issues.
ill check about the S-function or how i can manipulate the timeseries in a way to achoeve what needed.
thanks for taking the time!!! Have a nice rest of day
"...another measurement method which yielded [10 x 77000] matrices and when he transposed them he got [77000 x 10] matrices with no issues. "
I don't see that behavior on transposition...
v=rand(7,2); % make a 2D sample instead of vector
t=0:6; % a time vector to coincide with the 7 dimension
ts=timeseries(v,t) % not transposed
timeseries Common Properties: Name: 'unnamed' Time: [7x1 double] TimeInfo: tsdata.timemetadata Data: [7x2 double] DataInfo: tsdata.datametadata
ts=timeseries(v.',t) % now try to transpose v
timeseries Common Properties: Name: 'unnamed' Time: [7x1 double] TimeInfo: tsdata.timemetadata Data: [2x1x7 double] DataInfo: tsdata.datametadata
timeseries still built a 3D array, it's just no longer 1x1xN, but the arrangement is still as was.
In the other app, what is the time vector length, the 10 or the 77,000?
If, instead of t being of length(v), it is one, then
v=rand(7,1); % the v vector
t=0; % use a one-element time series (whatever that is)
ts=timeseries(v,t)
timeseries Common Properties: Name: 'unnamed' Time: [1x1 double] TimeInfo: tsdata.timemetadata Data: [7x1 double] DataInfo: tsdata.datametadata
ts=timeseries(v.',t)
timeseries Common Properties: Name: 'unnamed' Time: [1x1 double] TimeInfo: tsdata.timemetadata Data: [1x7 double] DataInfo: tsdata.datametadata
This will, indeed return a 1D row vector.
Now, only you can determine if that is what you really need or not; it doesn't make much sense from the outside looking in, but it may to you knowing what it is that is being modeled.
time vector is 77k, 10 is the number of channels
That's already very promising ngl, im a bit too fried to look into today to be honest. I will check it out tomorrow and come back to you! Thank you lots for the help. i really appreciate it.
v=rand(77000,10);
ts=timeseries(v)
timeseries Common Properties: Name: 'unnamed' Time: [77000x1 double] TimeInfo: tsdata.timemetadata Data: [77000x10 double] DataInfo: tsdata.datametadata
v=v.'; % transpose the original to be 10 x 77K; iow the channels first, not time
ts=timeseries(v)
timeseries Common Properties: Name: 'unnamed' Time: [10x1 double] TimeInfo: tsdata.timemetadata Data: [10x77000 double] DataInfo: tsdata.datametadata
ts=timeseries(v.')
timeseries Common Properties: Name: 'unnamed' Time: [77000x1 double] TimeInfo: tsdata.timemetadata Data: [77000x10 double] DataInfo: tsdata.datametadata
There's the issue -- if the original data are ordered by channel X time, the default behavior of timeseries is to consider the first dimension as the time and the second as the observations/channels. You get back a data array that is the same height as whatever is interpreted as the time, but the restriction on the first or last dimension matching the length of the time series is catching you out in the default case where timeseries can't comprehend there being only one time point in a time series and so takes the second dimension as the size of the time vector and then has to fix up the dimension to match the design rule.
v=rand(1,7);
ts=timeseries(v)
timeseries Common Properties: Name: 'unnamed' Time: [7x1 double] TimeInfo: tsdata.timemetadata Data: [1x1x7 double] DataInfo: tsdata.datametadata
That's where the only way you can force the row vector as a return using the timeseries object will be to force the one-element time as earlier shown...
ts=timeseries(v,0) % a non-default one-element time, too...
timeseries Common Properties: Name: 'unnamed' Time: [1x1 double] TimeInfo: tsdata.timemetadata Data: [1x7 double] DataInfo: tsdata.datametadata
Hi again,
So i did get what I want in the end thanks to this last message. By writing
Rk = timeseries(parentDataset.reflectometry.density',parentDataset.reflectometry.time(1));
Which shouldve been the right thing to do all along since data is measured within 1 time step and not 7. But using one element, like you advised, nade ne realize this and it fixed the issue, so thank you A LOT. I really appreciate your help!
dpb
dpb on 24 Jul 2025
Edited: dpb on 25 Jul 2025
Ah! Then the supposition of one observation turns out to be correct, after all. Kewl. Glad to have been able to help; I don't quite understand the reason underlying the behavior of creating the 3D array instead that is somewhat confusing.

Sign in to comment.

More Answers (0)

Products

Release

R2025a

Tags

Asked:

on 23 Jul 2025

Edited:

dpb
on 26 Jul 2025

Community Treasure Hunt

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

Start Hunting!