Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Matrix

Subject: Matrix

From: Jerry

Date: 14 Jun, 2012 17:31:07

Message: 1 of 15

Hello,

"S" is a Simdata or a structure data. The size of "S" is (1x98). Under each subset of S={S1, S2,...S98}, there is a data={d1, d2,...d98}, in that the size of the data is not the same, see below:

d1 <177x100 double>
d2 <191x100 double>
.
.
d98 <103x100 double>
 
>> R = size(S,2);
for i=1:R
    D {i}=S (i).Data ; %#ok<SAGROW>
end
>> Data=cell2mat (D);

I need to create the "Data". Since the size of {d1, d2,...d98} are not the same, I got following error:

??? Error using ==> cat
CAT arguments dimensions are not consistent.
Error in ==> cell2mat at 81
            m{n} = cat(2,c{n,:});

How may I fix this problem?

Thanks,

Jerry

Subject: Matrix

From: dpb

Date: 14 Jun, 2012 18:55:44

Message: 2 of 15

On 6/14/2012 12:31 PM, Jerry wrote:
> Hello,
>
> "S" is a Simdata or a structure data. The size of "S" is (1x98). Under
> each subset of S={S1, S2,...S98}, there is a data={d1, d2,...d98}, in
> that the size of the data is not the same, see below:
>
> d1 <177x100 double>
> d2 <191x100 double>
> ..
> ..
> d98 <103x100 double>
>
>>> R = size(S,2);
> for i=1:R
> D {i}=S (i).Data ; %#ok<SAGROW>
> end
>>> Data=cell2mat (D);
>
> I need to create the "Data". Since the size of {d1, d2,...d98} are not
> the same, I got following error:
>
> ??? Error using ==> cat
> CAT arguments dimensions are not consistent.
> Error in ==> cell2mat at 81
> m{n} = cat(2,c{n,:});
>
> How may I fix this problem?

Don't you want

cat(1,S(i).Data)

???

--

Subject: Matrix

From: Jerry

Date: 14 Jun, 2012 19:13:06

Message: 3 of 15

dpb <none@non.net> wrote in message <jrdc3f$ld2$2@speranza.aioe.org>...
> On 6/14/2012 12:31 PM, Jerry wrote:
> > Hello,
> >
> > "S" is a Simdata or a structure data. The size of "S" is (1x98). Under
> > each subset of S={S1, S2,...S98}, there is a data={d1, d2,...d98}, in
> > that the size of the data is not the same, see below:
> >
> > d1 <177x100 double>
> > d2 <191x100 double>
> > ..
> > ..
> > d98 <103x100 double>
> >
> >>> R = size(S,2);
> > for i=1:R
> > D {i}=S (i).Data ; %#ok<SAGROW>
> > end
> >>> Data=cell2mat (D);
> >
> > I need to create the "Data". Since the size of {d1, d2,...d98} are not
> > the same, I got following error:
> >
> > ??? Error using ==> cat
> > CAT arguments dimensions are not consistent.
> > Error in ==> cell2mat at 81
> > m{n} = cat(2,c{n,:});
> >
> > How may I fix this problem?
>
> Don't you want
>
> cat(1,S(i).Data)
>
> ???
>
> --

OK, I changed the loop in this way,

for i=1:NumResult
    D {i}=cat(1,S(i).Data); %#ok<SAGROW>
end

I still got this error, see:

??? Error using ==> cat
CAT arguments dimensions are not consistent.

Error in ==> cell2mat at 81
            m{n} = cat(2,c{n,:});

Thanks,

Jerry

Subject: Matrix

From: dpb

Date: 14 Jun, 2012 19:53:32

Message: 4 of 15

On 6/14/2012 2:13 PM, Jerry wrote:
...

> OK, I changed the loop in this way,
>
> for i=1:NumResult
> D {i}=cat(1,S(i).Data); %#ok<SAGROW>
> end
>
> I still got this error, see:
>
> ??? Error using ==> cat
> CAT arguments dimensions are not consistent.
>
> Error in ==> cell2mat at 81
> m{n} = cat(2,c{n,:});
...

Where's the cell2mat() call? I don't think you want that here at all.

--

Subject: Matrix

From: Jerry

Date: 14 Jun, 2012 20:05:07

Message: 5 of 15

dpb <none@non.net> wrote in message <jrdffr$u89$2@speranza.aioe.org>...
> On 6/14/2012 2:13 PM, Jerry wrote:
> ...
>
> > OK, I changed the loop in this way,
> >
> > for i=1:NumResult
> > D {i}=cat(1,S(i).Data); %#ok<SAGROW>
> > end
> >
> > I still got this error, see:
> >
> > ??? Error using ==> cat
> > CAT arguments dimensions are not consistent.
> >
> > Error in ==> cell2mat at 81
> > m{n} = cat(2,c{n,:});
> ...
>
> Where's the cell2mat() call? I don't think you want that here at all.
>
> --
Here is this command:

Data=cell2mat (D);

Once I applied it, I got the error.

Jerry

Subject: Matrix

From: dpb

Date: 14 Jun, 2012 20:17:01

Message: 6 of 15

On 6/14/2012 3:05 PM, Jerry wrote:
> dpb <none@non.net> wrote in message <jrdffr$u89$2@speranza.aioe.org>...
>> On 6/14/2012 2:13 PM, Jerry wrote:
>> ...
>>
>> > OK, I changed the loop in this way,
>> >
>> > for i=1:NumResult
>> > D {i}=cat(1,S(i).Data); %#ok<SAGROW>
>> > end
>> >
>> > I still got this error, see:
>> >
>> > ??? Error using ==> cat
>> > CAT arguments dimensions are not consistent.
>> >
>> > Error in ==> cell2mat at 81
>> > m{n} = cat(2,c{n,:});
>> ...
>>
>> Where's the cell2mat() call? I don't think you want that here at all.
>>
>> --
> Here is this command:
>
> Data=cell2mat (D);
> Once I applied it, I got the error.

Well, yes, that's what I'm saying to get rid of...use cat() directly
instead.

"The contents of C must support concatenation into a hyperrectangle."

It appears from the previous error it's trying to do on a row-wise
manner and the sizes aren't commensurate in that directions.

The best you can do is to concatenate vertically as they do seem to have
the same number of columns.

It seems to me this may be a limitation/bug(?) in cell2mat(); at least
worth asking if this is by design when there is a hyper-rectangle that
is possible in this case being Nx100. If there's a proscription against
this case by design at a minimum it seems the documentation ought to say
so; I don't see that it does.

Again I must note that cell2mat() postdates my release of Matlab so all
I know is what I can tell from reading the current release doc.

--

Subject: Matrix

From: Jerry

Date: 14 Jun, 2012 20:54:07

Message: 7 of 15

OK, let's do it

N = size(S,2);
for i=1:N
    D {i}=S(i).Data ; %#ok<SAGROW>
end

Data=cat(1,D);

But, there is no different between "D" and "Data".

Jerry

dpb <none@non.net> wrote in message <jrdgrs$2jo$1@speranza.aioe.org>...
> On 6/14/2012 3:05 PM, Jerry wrote:
> > dpb <none@non.net> wrote in message <jrdffr$u89$2@speranza.aioe.org>...
> >> On 6/14/2012 2:13 PM, Jerry wrote:
> >> ...
> >>
> >> > OK, I changed the loop in this way,
> >> >
> >> > for i=1:NumResult
> >> > D {i}=cat(1,S(i).Data); %#ok<SAGROW>
> >> > end
> >> >
> >> > I still got this error, see:
> >> >
> >> > ??? Error using ==> cat
> >> > CAT arguments dimensions are not consistent.
> >> >
> >> > Error in ==> cell2mat at 81
> >> > m{n} = cat(2,c{n,:});
> >> ...
> >>
> >> Where's the cell2mat() call? I don't think you want that here at all.
> >>
> >> --
> > Here is this command:
> >
> > Data=cell2mat (D);
> > Once I applied it, I got the error.
>
> Well, yes, that's what I'm saying to get rid of...use cat() directly
> instead.
>
> "The contents of C must support concatenation into a hyperrectangle."
>
> It appears from the previous error it's trying to do on a row-wise
> manner and the sizes aren't commensurate in that directions.
>
> The best you can do is to concatenate vertically as they do seem to have
> the same number of columns.
>
> It seems to me this may be a limitation/bug(?) in cell2mat(); at least
> worth asking if this is by design when there is a hyper-rectangle that
> is possible in this case being Nx100. If there's a proscription against
> this case by design at a minimum it seems the documentation ought to say
> so; I don't see that it does.
>
> Again I must note that cell2mat() postdates my release of Matlab so all
> I know is what I can tell from reading the current release doc.
>
> --

Subject: Matrix

From: Steven_Lord

Date: 14 Jun, 2012 21:30:22

Message: 8 of 15



"dpb" <none@non.net> wrote in message news:jrdgrs$2jo$1@speranza.aioe.org...
> On 6/14/2012 3:05 PM, Jerry wrote:
>> dpb <none@non.net> wrote in message <jrdffr$u89$2@speranza.aioe.org>...
>>> On 6/14/2012 2:13 PM, Jerry wrote:
>>> ...
>>>
>>> > OK, I changed the loop in this way,
>>> >
>>> > for i=1:NumResult
>>> > D {i}=cat(1,S(i).Data); %#ok<SAGROW>
>>> > end
>>> >
>>> > I still got this error, see:
>>> >
>>> > ??? Error using ==> cat
>>> > CAT arguments dimensions are not consistent.
>>> >
>>> > Error in ==> cell2mat at 81
>>> > m{n} = cat(2,c{n,:});
>>> ...
>>>
>>> Where's the cell2mat() call? I don't think you want that here at all.
>>>
>>> --
>> Here is this command:
>>
>> Data=cell2mat (D);
>> Once I applied it, I got the error.
>
> Well, yes, that's what I'm saying to get rid of...use cat() directly
> instead.
>
> "The contents of C must support concatenation into a hyperrectangle."
>
> It appears from the previous error it's trying to do on a row-wise manner
> and the sizes aren't commensurate in that directions.
>
> The best you can do is to concatenate vertically as they do seem to have
> the same number of columns.

Yes, I know that Jerry said/implied in his original message that they all
had 100 columns. Trust but verify ;)

Jerry, can you do me a favor and show the group what this code displays?


R = size(S,2);
sizesOfData = zeros(R, 1);
for i=1:R
    D {i}=S (i).Data ; %#ok<SAGROW>
    sizesOfData(i) = size(D{i}, 2);
end
checkSameNumberOfColumns = all(sizesOfData == sizesOfData(1))


My suspicion is that checkSameNumberOfColumns is false, which would mean
that one of your struct array fields has a different number of columns and
you should go back to the code that generated them and determine why. You
can use this to determine which fields have the different number of columns:

find(sizesOfData ~= sizesOfData(1))

> It seems to me this may be a limitation/bug(?) in cell2mat(); at least
> worth asking if this is by design when there is a hyper-rectangle that is
> possible in this case being Nx100. If there's a proscription against this
> case by design at a minimum it seems the documentation ought to say so; I
> don't see that it does.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Matrix

From: Steven_Lord

Date: 14 Jun, 2012 21:31:48

Message: 9 of 15



"Jerry " <jerrycholo@gmail.com> wrote in message
news:jrdj1f$2bu$1@newscl01ah.mathworks.com...
> OK, let's do it
>
> N = size(S,2);
> for i=1:N
> D {i}=S(i).Data ; %#ok<SAGROW>
> end
>
> Data=cat(1,D);
>
> But, there is no different between "D" and "Data".

That's correct. You're passing the whole cell array as the one non-dimension
input to CAT, and so it concatenates that cell array with ... nothing else,
since there was no other data input.

That's different from:

Data=cat(1,D{:});

which is equivalent to:

Data = cat(1, D{1}, D{2}, D{3}, ...);

Search the documentation for the term "comma separated list" for more
information.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Matrix

From: Jerry

Date: 14 Jun, 2012 22:31:06

Message: 10 of 15

OK, it works, but it is not what I want. It seems that "cat" function destroyed dimensionality of the data. Is it possible to have a data with these dimensions (98x191x100), in that difference between Cs (columns that have less than 191 data point) and 191 would fill out by "NAN".

Thanks,

Jerry

"Steven_Lord" <slord@mathworks.com> wrote in message <jrdl84$bvu$1@newscl01ah.mathworks.com>...
>
>
> "Jerry " <jerrycholo@gmail.com> wrote in message
> news:jrdj1f$2bu$1@newscl01ah.mathworks.com...
> > OK, let's do it
> >
> > N = size(S,2);
> > for i=1:N
> > D {i}=S(i).Data ; %#ok<SAGROW>
> > end
> >
> > Data=cat(1,D);
> >
> > But, there is no different between "D" and "Data".
>
> That's correct. You're passing the whole cell array as the one non-dimension
> input to CAT, and so it concatenates that cell array with ... nothing else,
> since there was no other data input.
>
> That's different from:
>
> Data=cat(1,D{:});
>
> which is equivalent to:
>
> Data = cat(1, D{1}, D{2}, D{3}, ...);
>
> Search the documentation for the term "comma separated list" for more
> information.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: Matrix

From: dpb

Date: 15 Jun, 2012 00:38:05

Message: 11 of 15

On 6/14/2012 5:31 PM, Jerry wrote:
> OK, it works, but it is not what I want. It seems that "cat" function
> destroyed dimensionality of the data. Is it possible to have a data with
> these dimensions (98x191x100), in that difference between Cs (columns
> that have less than 191 data point) and 191 would fill out by "NAN".
...

Sure it's possible but you'll have to programmatically augment the
smaller submatrices to make them the same size before trying to smoosh
them together--there's no provision for that.

--

Subject: Matrix

From: Jerry

Date: 15 Jun, 2012 16:28:07

Message: 12 of 15

dpb <none@non.net> wrote in message <jre05f$4aq$1@speranza.aioe.org>...
> On 6/14/2012 5:31 PM, Jerry wrote:
> > OK, it works, but it is not what I want. It seems that "cat" function
> > destroyed dimensionality of the data. Is it possible to have a data with
> > these dimensions (98x191x100), in that difference between Cs (columns
> > that have less than 191 data point) and 191 would fill out by "NAN".
> ...
>
> Sure it's possible but you'll have to programmatically augment the
> smaller submatrices to make them the same size before trying to smoosh
> them together--there's no provision for that.
>
> --

OK, may someone help me to write down these command lines to make these sub-matrices that have same size?

Thanks,

Jerry

Subject: Matrix

From: dpb

Date: 15 Jun, 2012 16:44:10

Message: 13 of 15

On 6/15/2012 11:28 AM, Jerry wrote:
> dpb <none@non.net> wrote in message <jre05f$4aq$1@speranza.aioe.org>...
>> On 6/14/2012 5:31 PM, Jerry wrote:
>> > OK, it works, but it is not what I want. It seems that "cat" function
>> > destroyed dimensionality of the data. Is it possible to have a data
>> with
>> > these dimensions (98x191x100), in that difference between Cs (columns
>> > that have less than 191 data point) and 191 would fill out by "NAN".
>> ...
>>
>> Sure it's possible but you'll have to programmatically augment the
>> smaller submatrices to make them the same size before trying to smoosh
>> them together--there's no provision for that.
>>
>> --
>
> OK, may someone help me to write down these command lines to make these
> sub-matrices that have same size?

Just build a loop that goes thru and compares size(x,2) to the desired
size (191?) and augments size by

aug=nan*ones(size(x,1), 191-size(x,2));
x=[x aug];

for each 'x' in the population.

Myriad of ways other as well, of course.

--

Subject: Matrix

From: Steven_Lord

Date: 15 Jun, 2012 17:02:14

Message: 14 of 15



"Jerry " <jerrycholo@gmail.com> wrote in message
news:jrfnqn$bff$1@newscl01ah.mathworks.com...
> dpb <none@non.net> wrote in message <jre05f$4aq$1@speranza.aioe.org>...
>> On 6/14/2012 5:31 PM, Jerry wrote:
>> > OK, it works, but it is not what I want. It seems that "cat" function
>> > destroyed dimensionality of the data. Is it possible to have a data
>> > with
>> > these dimensions (98x191x100), in that difference between Cs (columns
>> > that have less than 191 data point) and 191 would fill out by "NAN".
>> ...
>>
>> Sure it's possible but you'll have to programmatically augment the
>> smaller submatrices to make them the same size before trying to smoosh
>> them together--there's no provision for that.
>>
>> --
>
> OK, may someone help me to write down these command lines to make these
> sub-matrices that have same size?

You could probably do something similar to the padding code given in message
3 of this thread:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/321014

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Matrix

From: Jerry

Date: 15 Jun, 2012 19:54:07

Message: 15 of 15

"Steven_Lord" <slord@mathworks.com> wrote in message <jrfpqm$kru$1@newscl01ah.mathworks.com>...
>
>
> "Jerry " <jerrycholo@gmail.com> wrote in message
> news:jrfnqn$bff$1@newscl01ah.mathworks.com...
> > dpb <none@non.net> wrote in message <jre05f$4aq$1@speranza.aioe.org>...
> >> On 6/14/2012 5:31 PM, Jerry wrote:
> >> > OK, it works, but it is not what I want. It seems that "cat" function
> >> > destroyed dimensionality of the data. Is it possible to have a data
> >> > with
> >> > these dimensions (98x191x100), in that difference between Cs (columns
> >> > that have less than 191 data point) and 191 would fill out by "NAN".
> >> ...
> >>
> >> Sure it's possible but you'll have to programmatically augment the
> >> smaller submatrices to make them the same size before trying to smoosh
> >> them together--there's no provision for that.
> >>
> >> --
> >
> > OK, may someone help me to write down these command lines to make these
> > sub-matrices that have same size?
>
> You could probably do something similar to the padding code given in message
> 3 of this thread:
>
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/321014
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

OK, let's use function of "supervertcat", however my "varargin" is not a vector or a matrix, it is a "cell". Since the rows of sub-matrices within "varargin" are not the same I had to modify the function in this way:

function C = supervertcat(varargin)
% C = supervertcat(A1, A2, ...)
nrow = cellfun('size',varargin{:},1);
maxrow = max(nrow);
for k = 1:nargin
    if nrow(k) < maxrow
        varargin{k}(end,maxrow) = nan;
    end
end
C = cat(1, varargin{:});
end

How may I modify this function to deal with a "cell"?

Thanks,

Jerry

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us