Trasform cell arrays vector into a double vector

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
madhan ravi el 13 de Jul. de 2019
Explicitly show what your t and y should contain.
I would like them to show the same as
% pluto_test = [0;0;0;0.005;0.1;0.1]
% pippo_test = [1;2;3;2;5;4]
but trhough the for loop
Let me know
AP
dpb
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
Andrea Pinto el 13 de Jul. de 2019
Editada: Andrea Pinto el 13 de Jul. de 2019
The issue is to extract the values from the cell arrays vector (n(k,1), 1) into a double vector (n * k, 1) and make the double vector follows the same pattern.
x{1,1} = cell array{1,1} = {x1, x2, x3}
y(1,1) = double(1,1) = [y1]
than after the trasformation there will be
xnew = [x1 ; x2 ; x3]
ynew = [y1 ; y1 ; y1]
Hope this clarify the issue
Best
AP
dpb
dpb el 13 de Jul. de 2019
Editada: dpb 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.

Iniciar sesión para comentar.

Respuestas (1)

dpb
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

Dear dpb, you are right that create a table add complexity (in this particular case). But, of course, this is a simplified case. The vectors I am managing are part of a more complex table which have both double/cell array/strings vectors.
My final intent is to
plot(t,y)
But considering that y is a "cell-array" I have seen that the easiest way to plot this graph is by trasforming a (k(m,1),1) cell-array vector in a (k*m,1) double vector and alligning "t" to have the same lenght of the k-vestors' y on every cycle.
I do not know if it exists an easier way to make the graph, but by look on internet extensively, I could not find a proper direct answer to my issues.
Hope this further clarifies the issue.
Let me know if there might be of any additional help.
Best
AP
dpb
dpb el 13 de Jul. de 2019
Editada: dpb 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
Andrea Pinto el 13 de Jul. de 2019
Dear dpb, there are not "consistent couple" because the vectors pippo / pluto derives from yet another loop where pippo was the value of the variable being cycled in a much more complex function and pluto were all the real value of the irr function. Therefore, it was not possible from the beginning to create a "pair value" for which for every pippo there was one and only one pluto. Given all the above, I need to create exactly what you were referring at: "consistent couples" (ps. unfortunately "t" is not a time-variable).
My solution first solution is, of course, straightforward but it is not feasible. There are hundred of rows in the A-table and each pluto cell-array might have a variable n >= 1 value inside. Therefore I must find a "for loop" solution (or another one that I did not yet come up with) able to do what I did "by hand" in a repetite yet efficient way.
I unfortunately cannot share with you much more, because this is the end of a thousand lines script with more than 100 formulas and I have tried to simplify as much as possible for the purpose of this question.
AP
dpb
dpb el 13 de Jul. de 2019
Editada: dpb 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
Andrea Pinto el 13 de Jul. de 2019
Both "t" and "y" are percentage value. "y" has been calculated through the "irr function" of some cash flow. "t" is one of many variables which, after many processing, participate to calculate the cash flow of the model and allows the irr function to calculate t = real(allrates) of function irr (which is an embedded function of matlab). Given all the above, it is logically possible to plot t vs y because they are both percentages. The meaning of the plot is beyond the logic of this post and would be way to complex to explain here.
My issue is to find a feasible solution to plot(t,y) and because the input of the function plot are two vectors of the same size, I was wondering how to create "couple pairs" (t1,y(1:n)) starting from the Table A (which is the thing most close to how the data looks on my matlab).
I wish I could go more in detaills, but I do really think it would only complicate the whole picture, rather than simplifying it. If you could be so kind to help me out to find a solution to this specific topic, I would really appreciate.
AP
dpb
dpb el 13 de Jul. de 2019
Editada: dpb 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
Andrea Pinto el 13 de Jul. de 2019
I do not get why you say so. If I have a table x (m,n) composed by two variables x1 and x2 where x1 is a double (single value) while x2 is a cell-array (vector of n elements inside a cell).
No matter how x1 and x2 where built, is there a way to extract the value in each "m" row of x2 and create one single vector "y" which has a lenght of (m*n,1)?
Can you help me in figuring out how to solve this problem? I would really appreciate.
AP
dpb
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.
dpb
dpb el 13 de Jul. de 2019
Editada: dpb el 13 de Jul. de 2019
"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
Andrea Pinto el 13 de Jul. de 2019
The code you provided does not do what I was aiming for.
I need that from each row of table A [A{:,2}] to extract the value in each cell-array and trasform this values in a vertical vector which is the sum of the value extracted (as I had shown above with "pluto_test" which is different from the "ANS" of your loop.
AP
_"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
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...
Dear dpb, I already told you about it.
It is not possible to modify the process before this step because the "cell-array" values come from a cycle using the "irr function" which has imbedded an "allrates" function which provide all (complex/real answers) in a cell-array. Therefore the structure "1 double to 1 cell-array" cannot be modifies.
Considering that I am using an hidden function of matlab, the final result of Table A cannot be modified. This is the reason why I am not providing you with the big picture because it would not change the fact that, in the end, i need to work out the issue directly in the form I have already written: through A.
By the way, I found half of the solution of what I need:
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
My issue now is how to calculate C = pippo_test, because how I wrote it now gives me an error.
Can you help to solve it out?
AP
In the end I found a suitable solution which does what I was aiming for. Thank you for your help. I post it here just FYI.
B = [];
C = [];
for k = 1:n
B = [B;cell2mat(A{k,'pluto'})];
C = [C;ones(size(cell2mat(A{k,'pluto'}),1),1)]*A{k,'pippo'};
end
dpb
dpb el 14 de Jul. de 2019
Editada: dpb 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.

Iniciar sesión para comentar.

Preguntada:

el 13 de Jul. de 2019

Editada:

dpb
el 14 de Jul. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by