MATLAB Answers

Trasform cell arrays vector into a double vector

2 views (last 30 days)
Andrea Pinto
Andrea Pinto on 13 Jul 2019
Edited: dpb on 14 Jul 2019
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 Comments
dpb
dpb on 13 Jul 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.

Sign in to comment.

Answers (1)

dpb
dpb on 13 Jul 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 Comments
dpb
dpb on 14 Jul 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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!