From: Zeeshan on
"Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <i0jtio$agm$1(a)fred.mathworks.com>...
> "Zeeshan " <zeeextra(a)yahoo.com> wrote in message <i0jrkt$44o$1(a)fred.mathworks.com>...
> > Hi Roger
> >
> > I want to plot a 2D mesh of spherical pore bodies connected with throats. The 2D mesh could be a diamond lattice. The spherical pore bodies is of volume ((4 &#960;/3 )r3 ) = l3 and the throat bodies are cylindrical which are of length l and have randomly chosen cross sectional area between 0 and l2. Also the volume of both pore and throat could be adjusted as desired.
> >
> > Can you help me in this ?
> - - - - - - - - - -
> That is not ethical, Zeeshan. You have tacked your own question onto another person's entirely different thread. That is not the way to get questions answered. Also you have entered exactly the same question with precisely the same wording (except for the "Hi Roger") in at least four other threads today and yesterday. This is definitely not the way to cause other people to be willing to help you. Duplicate threads are very much frowned upon in this newsgroup. I certainly don't intend to help you.
--------
Sorry if it bothers you
Zeeshan
From: Volkan on
"Roger Stafford" <ellieandrogerxyzzy(a)mindspring.com.invalid> wrote in message <i0jpbo$97t$1(a)fred.mathworks.com>...
> "Volkan " <volkaned(a)hotmail.com> wrote in message <i0jemg$hka$1(a)fred.mathworks.com>...
> > Hi,
> >
> > I am trying to avoid for loops but I couldn't find an easy solution for the problem below:
> >
> > p1hf = [1,2;3,4];
> > nb = 2; % It might go up to 100 or so need a smarter solution
> > p2hf = zeros(nb, nb, nb, nb);
> > for a=1:nb
> > for b=1:nb
> > for c=1:nb
> > for d=1:nb
> > p2hf(a,b,c,d) = 2*p1hf(a,b)*p1hf(c,d) - p1hf(a,c)*p1hf(b,d);
> > end
> > end
> > end
> > end
> >
> > p2hf is 4-D. Is there an easy way to avoid these loops?
> >
> > Your help will be greatly appreciated.
> - - - - - - - - -
> I think this does the same thing, but I'm not sure any time is saved.
>
> p2hf = reshape(reshape(p1hf,[],1)*reshape(p1hf,1,[]),nb,nb,nb,nb);
> p2hf = 2*p2hf-permute(p2hf,[1,3,2,4]);
>
> Roger Stafford

Thank you so much Roger. I think you are right about cpu usage. It doesn't really save time when nb > 50.

I tried to run it with nb = 2 ( your algorithm was slightly faster), nb =5 ( mine was slightly faster), nb =75 (mine was much faster), and nb =100 (out of memory).

Your solution is brilliant though. I gained a new perspective to consider when dealing with these kinds of problems. Thank you so much again.
From: Jan Simon on
Dear Volkan,

> p1hf = [1,2;3,4];
> nb = 2; % It might go up to 100 or so need a smarter solution
> p2hf = zeros(nb, nb, nb, nb);
> for a=1:nb
> for b=1:nb
> for c=1:nb
> for d=1:nb
> p2hf(a,b,c,d) = 2*p1hf(a,b)*p1hf(c,d) - p1hf(a,c)*p1hf(b,d);
> end
> end
> end
> end

Do you just want to omit the loops or do you need more speed?
In the later case you could try this:

p1hf = [1,2; 3,4];
nb = 2; % It might go up to 100 or so need a smarter solution
p2hf = zeros(nb, nb, nb, nb);
for a = 1:nb
for b = 1:nb
t1 = 2*p1hf(a,b);
for c = 1:nb
p2hf(a,b,c, 1:nb) = t1*p1hf(c, 1:nb) - p1hf(a,c)*p1hf(b, 1:nb);
end
end
end

If the dimensions are matching, the "1:nb" index can be omitted:
p2hf(a,b,c, :) = t1*p1hf(c, :) - p1hf(a,c)*p1hf(b, :);

You can replace the loop over c also:
...
for a = 1:nb
for b = 1:nb
t1 = 2*p1hf(a,b);
p2hf(a,b, :, :) = t1*p1hf - transpose(p1hf(a, :)) * p1hf(b, :);
end
end

(Please test this - I do not have access to Matlab on this computer).

Good luck, Jan
From: Matt Fig on
Try this:

p3 = bsxfun(@times,2*p1hf,reshape(p1hf,1,1,nb,nb)) -...
bsxfun(@times,reshape(p1hf,nb,1,nb),reshape(p1hf,1,nb,1,nb));
From: Volkan on
"Matt Fig" <spamanon(a)yahoo.com> wrote in message <i0klds$dl1$1(a)fred.mathworks.com>...
> Try this:
>
> p3 = bsxfun(@times,2*p1hf,reshape(p1hf,1,1,nb,nb)) -...
> bsxfun(@times,reshape(p1hf,nb,1,nb),reshape(p1hf,1,nb,1,nb));

Dear Matt,

It seems that bsxfun is the fastest among the suggested routines taking care of the problem in hand. It is slightly faster than for loops, while keeping the code elegant and compact.

Thanks you all for your help.