Trasform cell arrays vector into a double vector
Mostrar comentarios más antiguos
Dear all, I am having difficulties in finding a solution to the following problem.
c1 = [0;0.005;0.01]; % the vector might be larger than 3 data points and cannot be determined upfront
c2 = {[1;2;3];[2];[5;4]}; % the dimension of the cell arrays might vary and cannot be determined upfront
A1 = array2table(c1,'VariableNames',{'pippo'});
A2 = array2table(c2,'VariableNames',{'pluto'});
A = [A1 A2];
pluto_c2m_1 = cell2mat(A{1,'pluto'});
pippo_c2m_1 = ones(size(pluto_c2m_1,1),1)*A{1,'pippo'};
pluto_c2m_2 = cell2mat(A{2,'pluto'});
pippo_c2m_2 = ones(size(pluto_c2m_2,1),1)*A{2,'pippo'};
pluto_c2m_3 = cell2mat(A{3,'pluto'});
pippo_c2m_3 = ones(size(pluto_c2m_3,1),1)*A{3,'pippo'};
pluto_test = [pluto_c2m_1;pluto_c2m_2;pluto_c2m_3];
pippo_test = [pippo_c2m_1;pippo_c2m_2;pippo_c2m_3];
% NB pluto_test & pippo_test do their work perfectly, they creare two vectors "double"
% pluto_test = [0;0;0;0.005;0.1;0.1]
% pippo_test = [1;2;3;2;5;4]
When I try to write this whole process with a for loop, i am not able to figure out how to write the whole process in a way that it works without Matlab going crazy. I have tried to write something like this:
n = size(A,1);
for k = 1:n
pluto_c2m(k) = (cell2mat(A{k,'pluto'}));
pippo_c2m(k) = (ones(size(pluto_c2m(k),1),1)*A{k,'pippo'});
end
y = pluto_c2m(:);
t = pippo_c2m(:);
But I receive this error:
% Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
Any help would really be appreciated, I cannot figure it out alone.
Best
AP
5 comentarios
madhan ravi
el 13 de Jul. de 2019
Explicitly show what your t and y should contain.
Andrea Pinto
el 13 de Jul. de 2019
dpb
el 13 de Jul. de 2019
So, what's the general problem definition? Augment the first data set to the length of the elements of the second as column vectors?
Andrea Pinto
el 13 de Jul. de 2019
Editada: Andrea Pinto
el 13 de Jul. de 2019
Then the first solution of the Answer appears the most straightforward route.
A general solution/function would have to test for who's the longer and all unless it is known a priori, but we don't have enough information on the input parameter boundaries to know just what are the possibilities to have to account for, specifically.
The Answer presumes it is known the cell array will contain at least as many elements as the vector so it is always the vector that needs augmentation.
Respuestas (1)
dpb
el 13 de Jul. de 2019
All the gyrations with tables are apparently totally unnecessary and just added complexity...
pluto_test=cell2mat(c2);
pippo_test=[zeros(numel(pluto_test)-numel(c1),1); c1];
But, if you insist on creating the composite table A, then something like
>> for i=1:2,A{:,i},end
ans =
0
0.0050
0.0100
ans =
3×1 cell array
{3×1 double}
{[ 2]}
{2×1 double}
>>
will let you retrieve the data you had to begin with and (with a fair amount of extra effort) determine which column is a cell array vis a vis a vector and manipulate such as above that could have done from the beginning.
Think we need more motivation for why the problem is posed as is and, perhaps a rethinking of the overall data structure is in order.
15 comentarios
Andrea Pinto
el 13 de Jul. de 2019
"My final intent is to plot(t,y)"
Well, that's yet another detail to deal with -- where does the independent variable t live and why aren't there consistent pairs of t,y already before we get to this point?
I think to have an overall reasonable solution we need to see more of the big picture -- I understand trying to simplify to ask a Q?, but sometimes that leads to oversimplification to where the essence of the actual problem is lost -- as in the above intitial solution is the obvious straightforward answer to the original questin as posed.
BTW, if the t is a time, you may well be better served by use of a timetable rather than regular table -- altho that would, I believe, require regularization of the input vectors on creation (which probably would not be a_bad_thing ™ to have done on creation again as noted above. "If you don't create a mess in the data table, then you don't have a mess to clean up later."
NB: For plotting, almost certainly you'll want any augmentation points to be NaN, not 0 so they'll be ignored by plot().
ADDENDUM:
Also, if the point is to just plot the various variables, why is there any need to do anything other than plot them as they are? There's no point in "making up" data and forcing everything into a particular size just for that purpose...plot what you have for each and go on.
Without the specifics for t, the following works for your example A table:
for i=1:size(A,2)
y=A{:,i};
if iscell(y), y=cell2mat(y); end
plot(y);
if i==1,hold on,end
end
Of course, the size of the first column is such as compared to the second you'll have to set
hAx=gca;
hAx.YScale='log';
to be able to see both curves at the same time...
Andrea Pinto
el 13 de Jul. de 2019
Well, the t value has to come from somewhere...where is it and how does it relate to the elements in the table rows? Without that, there's no way to plot() the results as you've stated.
Save this wondrous table as a .mat file and attach it -- with the explanation of how to define the correlation between the independent and dependent variables positions. That has to be possible somehow or the problem is insoluble.
The basic solution is, I am virtually positive unless the actual table shows something totally unexpected, a perturbation of the above sample code.
ADDENDUM:
Thought just occurred to me -- is the reason for the cell array content having more than one element per table row that there are multiple estimates/measurements/data for each of those rows and the t variable correlates with the table row, maybe??? That would be pretty trivial to deal with if that were to be the case.
Andrea Pinto
el 13 de Jul. de 2019
As said, to proceed I need the table A as a .mat file so can see it and the logic that matches the variable t with the associated y
What it means is your business, but there has to be some way to derive the connection between which element of the y vector is associated with which t or the problem is insoluble (programmatically, anyways).
As the example above shows, there's no need for plotting to create a single array as you've been trying to do; that simply complicates matters more than needs be...again, on the presumption your statement that plotting is the goal is accurate and there's not some other requirement for an actual array.
Andrea Pinto
el 13 de Jul. de 2019
dpb
el 13 de Jul. de 2019
Because I can't visualize the solution from the description alone, sorry-- I need actual piece of data to work on. I can't see from your description where the independent variable is defined or how it is related to the various columns of different lengths--that has to be key to any solution unless there's another set of unique indepedent variable columns that are associated with the y columns?
Remember, you know all about what you're trying to do and how all this stuff is put together; all we know here is what we're shown and told--we don't have the benefit of the other background knowledge.
I'm sure there's a way to produce a set of vectors that can be plotted; I illustrated that in the previous but it doesn't handle associating particular values of the cells with the appropriate independent variable nor does it relate the short colums values of independent variables to what should those values be.
"way to extract the value in each "m" row of x2 and create one single vector "y" which has a lenght [sic] of (m*n,1)?"
Well, that's just the code I showed you above of cell2mat if X2 is a cell array. But, in the table if X1 is a double, it can have only M < N values so we again have the "many to one" or "one to many" conundrum to solve that hasn't been defined.
And, the length isn't M*N unless all M rows have N elements which is not what your example showed--the length is sum(N(m)) for the M rows which is actually longer than M.
For the example you gave
>> numel(y)
ans =
6
>> A
A =
3×2 table
pippo pluto
_____ ____________
0 [3×1 double]
0.005 [ 2]
0.01 [2×1 double]
>>
Needless to say, the above dichotomies leave me scratching my head as to what the real answer is to be and, particularly that seems to revolve around who/what is the independent variable...how many values of it there are total and how each of the cell elements is related to it. plot() expects a 1:1 association for each x,y pair -- how to build that from the data you have is the (as yet unexplained) question.
Andrea Pinto
el 13 de Jul. de 2019
dpb
el 13 de Jul. de 2019
_"I had shown above with "pluto_test" which is different from the "ANS" of your loop."_
Sorry, but I beg to differ. I just repasted your sample code from first posting and ran it again--the result is
>> pluto_test
pluto_test =
1
2
3
2
5
4
>> for i=2:size(A,2),y=A{:,i}; if iscell(y), y=cell2mat(y);end,plot(y);if i==1,hold on,end,end
>> y
y =
1
2
3
2
5
4
>> all(pluto_test==y)
ans =
logical
1
>>
The result is identical.
Why can't you show where the independent variable(s) t come from and how they relate and/or just attach a .mat file with your data to be able to see it?
What would you expect to plot pluto_test against? That data has to come from somewhere and how it is defined/stored has to be the key on building the plotting vector(s).
In your A table, is X1 also to be plotted against some other set of t I gather from the initial post? So, there are in this case 3 of X1 and 6 of X2 so there have to be commensurate t values as well...
I'm more than happy to try to help, but we've yet to get an understanding of what the groundrules are for getting to that objective.
dpb
el 14 de Jul. de 2019
"...show where the independent variable(s) t come from"
By which I don't mean how your code is generating them but where they are in the data structure or how they're stored independent of this data structure so can write appropriate code to access them...and, of course, the question of how those relate to the values in the various columns with variable numbers of elements is still the key missing link...
Andrea Pinto
el 14 de Jul. de 2019
Andrea Pinto
el 14 de Jul. de 2019
>> B=[];
>> n = size(A,1);
for k = 1:n
B = [B;cell2mat(A{k,'pluto'})]; % B = pluto_test
%C(k) = [C;ones(size(B(k),1),1)]*A{k,'pippo'};
end
>> B
B =
1
2
3
2
5
4
>> all(B==y)
ans =
logical
1
>>
Which is no different than the previous result I provided which is much simpler, not dependent upon the name nor doing dynamic reallocation (a very overhead-intensive operation to be avoided if at all possible which is possible here).
"Dear dpb, I already told you about it."
Dear AP, No, you've adamantly REFUSED to say anything definitive about where the t variable against which you're trying to plot() is stored and how there is going to be a correlation against it by which you can. The two vectors (t,y) will BY NECESSITY have to be consonant in size or all is for naught as the call to plot() will fail. (And, coincidentally each y will need to be associated with the proper t sequentially or the plot will not make any sense even if the lengths match but the data aren't in proper sequence.)
It's absolutely immaterial how these values were computed -- they can be as obscure and magical as you wish, but they have to be stored somewhere and there will have to be a 1:1 meeting of one of them vs one y value before any plot will show up....and if, indeed they can be complex variables, plot() will ignore anything except the real part so you'll have to have some plan for dealing with that as well.
Without how the two are to be associated I believe I've done all I can -- the code snippet I provided earlier above will return the data from the columns as individual column vectors; make of it what you will.
Categorías
Más información sobre Resampling Techniques en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!