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:
extending sparse to multi dimensional array/matrix ?

Subject: extending sparse to multi dimensional array/matrix ?

From: Lin

Date: 13 Mar, 2009 16:47:01

Message: 1 of 6

Hi all.

say i have

a = [0 11 12 0; 0 0 0 0; 11 12 0 0]
b = [0 21 22 0; 0 0 0 0; 21 22 0 0]
c = [0 31 32 0; 0 0 0 0; 31 32 0 0]
d = [0 41 42 0; 0 0 0 0; 41 42 0 0]

e = cat(4,a,b,c,d)

S = sparse(e)

it says undefined function for sparse. Can this be realised by another similar function? Thanks for helping.

Subject: extending sparse to multi dimensional array/matrix ?

From: John D'Errico

Date: 13 Mar, 2009 17:04:01

Message: 2 of 6

"Lin " <shenqlv@yahoo.com> wrote in message <gpe2m5$ov3$1@fred.mathworks.com>...
> Hi all.
>
> say i have
>
> a = [0 11 12 0; 0 0 0 0; 11 12 0 0]
> b = [0 21 22 0; 0 0 0 0; 21 22 0 0]
> c = [0 31 32 0; 0 0 0 0; 31 32 0 0]
> d = [0 41 42 0; 0 0 0 0; 41 42 0 0]
>
> e = cat(4,a,b,c,d)
>
> S = sparse(e)
>
> it says undefined function for sparse. Can this be realised by another similar function? Thanks for helping.

No. Sparse is only defined for 2-d arrays and
vectors. However, nothing stops you from using
cell arrays of sparse matrices, at least if it is only
for efficiency of storage.

John

Subject: extending sparse to multi dimensional array/matrix ?

From: Lin

Date: 13 Mar, 2009 17:25:02

Message: 3 of 6

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <gpe3m1$2a5$1@fred.mathworks.com>...
> "Lin " <shenqlv@yahoo.com> wrote in message <gpe2m5$ov3$1@fred.mathworks.com>...

> No. Sparse is only defined for 2-d arrays and
> vectors. However, nothing stops you from using
> cell arrays of sparse matrices, at least if it is only
> for efficiency of storage.
>
> John

Thanks for replying.. Are you referring to something as shown?
                                      
a = cell(3,3);
b = cell(3,3);

a{1,1} = [1;0]
b{2,3} = [1;1]

c= cat (3, a, b);

i got an error using this too.. In addition, i need to use celldisp so that i can see the content. What could be done to resolve the 2 problems? Thanks for helping.

Subject: extending sparse to multi dimensional array/matrix ?

From: Lin

Date: 15 Mar, 2009 13:23:01

Message: 4 of 6

"Lin " <shenqlv@yahoo.com> wrote in message <gpe4te$m0c$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <gpe3m1$2a5$1@fred.mathworks.com>...
> > "Lin " <shenqlv@yahoo.com> wrote in message <gpe2m5$ov3$1@fred.mathworks.com>...
>
> > No. Sparse is only defined for 2-d arrays and
> > vectors. However, nothing stops you from using
> > cell arrays of sparse matrices, at least if it is only
> > for efficiency of storage.
> >
> > John
>
> Thanks for replying.. Are you referring to something as shown?
>
> a = cell(3,3);
> b = cell(3,3);
>
> a{1,1} = [1;0]
> b{2,3} = [1;1]
>
> c= cat (3, a, b);
>
> i got an error using this too.. In addition, i need to use celldisp so that i can see the content. What could be done to resolve the 2 problems? Thanks for helping.

I have tried other means, but I failed to resolve my problem.

Say I have the following data as follows:

T1 = {0 11 12 0 0 0 0 0 0 0 0;
      0 0 0 0 0 11 0 0 0 0 0;
      0 0 0 0 0 12 0 0 0 0 0;
      0 0 0 0 0 0 0 0 0 0 0;
      0 0 0 0 0 0 0 0 0 0 0};
 
T2 = {0 21 0 22 0 0 0 0 0 0 0;
      0 0 0 0 0 0 21 0 0 0 0;
      0 0 0 0 0 0 0 0 0 0 0;
      0 0 0 0 0 0 22 0 0 0 0;
      0 0 0 0 0 0 0 0 0 0 0};
 
T3 = {0 31 0 0 32 0 0 0 0 0 0;
      0 0 0 0 0 0 0 31 0 0 0;
      0 0 0 0 0 0 0 0 0 0 0;
      0 0 0 0 0 0 0 0 0 0 0;
      0 0 0 0 0 0 0 32 0 0 0};

Is there a function that can combine Sparse and Cat into one?

%Example
Combine = cat (3,T1,T2,T3)
S = sparse(Combine)

Or are there better solutions out there ? Your help is greatly appreciated.

Subject: extending sparse to multi dimensional array/matrix ?

From: Matt

Date: 15 Mar, 2009 13:36:02

Message: 5 of 6

"Lin " <shenqlv@yahoo.com> wrote in message <gpivfl$4hq$1@fred.mathworks.com>...

> Say I have the following data as follows:
>
> T1 = {0 11 12 0 0 0 0 0 0 0 0;
> 0 0 0 0 0 11 0 0 0 0 0;
> 0 0 0 0 0 12 0 0 0 0 0;
> 0 0 0 0 0 0 0 0 0 0 0;
> 0 0 0 0 0 0 0 0 0 0 0};
>
> T2 = {0 21 0 22 0 0 0 0 0 0 0;
> 0 0 0 0 0 0 21 0 0 0 0;
> 0 0 0 0 0 0 0 0 0 0 0;
> 0 0 0 0 0 0 22 0 0 0 0;
> 0 0 0 0 0 0 0 0 0 0 0};
>
> T3 = {0 31 0 0 32 0 0 0 0 0 0;
> 0 0 0 0 0 0 0 31 0 0 0;
> 0 0 0 0 0 0 0 0 0 0 0;
> 0 0 0 0 0 0 0 0 0 0 0;
> 0 0 0 0 0 0 0 32 0 0 0};
>
> Is there a function that can combine Sparse and Cat into one?
>
> %Example
> Combine = cat (3,T1,T2,T3)
> S = sparse(Combine)
>
> Or are there better solutions out there ? Your help is greatly appreciated.


How about the following

S{1}=sparse(cell2mat(T1));
S{2}=sparse(cell2mat(T2));
S{3}=sparse(cell2mat(T3));

Better yet, instead of first creating cell arrays T1, T2, and T3, create the S{i} directly.

You haven't said what you're trying to do and why, so all anyone can assume is that you're trying to find a way of efficiently storing a set of sparse slices in a single object.

Subject: extending sparse to multi dimensional array/matrix ?

From: Lin

Date: 15 Mar, 2009 14:10:03

Message: 6 of 6

"Matt " <xys@whatever.com> wrote in message <gpj082$j6f$1@fred.mathworks.com>...
> "Lin " <shenqlv@yahoo.com> wrote in message <gpivfl$4hq$1@fred.mathworks.com>...
>
> How about the following
>
> S{1}=sparse(cell2mat(T1));
> S{2}=sparse(cell2mat(T2));
> S{3}=sparse(cell2mat(T3));
>
> Better yet, instead of first creating cell arrays T1, T2, and T3, create the S{i} directly.
>
> You haven't said what you're trying to do and why, so all anyone can assume is that you're trying to find a way of efficiently storing a set of sparse slices in a single object.

Thanks for replying. I am trying to combine all the T's into 1 so that i can use them in a loop.
Using my previous example,

combine = cat (3, T1,T2,T3)
for x=1:6
for for y = 1:7

%search through combine for a particular data, say '11'
if found disp('content found')
perform some other operations
else disp('No such content')
perform some other operations

end
end

I need to squeeze all the zeros so that the disp will be more compact and precise. I hope this is clear enough to all readers. By the way, is the solution that you had posted earlier still relevant in this context? Or is there a more efficient solution? Thanks for helping.

Tags for this Thread

No tags are associated with 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