From: Greig on
I have translated a program from Fortran to MATLAB with the aim of speeding it up using vectorization, but I'm needing a little help to get it running smoothly. It runs OK with scalars, but there are a few issues with vectorization.

First, this code snippet works, but is there a way of doing it without the loop?

nleft(length(time))=zeros;
for i=1:1:length(time)
nleft(i)=find(tknts(5:nspl+1)<time(i), 1,'last')+4;
end

where 'nleft' and 'time' are 1 x m vectors. I'm intending to use large m, so doing away with the loop would be a great help.

Second, I'm having difficultly handling data from a function.

spl(nsplt,m)=zeros;
spl(nleft-3:nleft,:)=bspline(tknts,time,jord,nleft);

Here, 'bspline' returns a 4 x m matrix and 'nleft' is still a 1 x m vector. What I want to do is assign the each column of the returned matrix into 'spl' starting at a specific row determined by the value in corresponding column of the vector 'nleft'. The starting rows are all different.
Currently, all values returned by the function are assigned to the same rows within 'spl', which is not what I want.

I hope this is the only issue I'll have with this program, but I'm sure I'll be in need of more advice.

Cheers
G
From: Bruno Luong on
"Greig " <greig(a)abc.com> wrote in message <i2b8cv$k7$1(a)fred.mathworks.com>...
> I have translated a program from Fortran to MATLAB with the aim of speeding it up using vectorization, but I'm needing a little help to get it running smoothly. It runs OK with scalars, but there are a few issues with vectorization.
>
> First, this code snippet works, but is there a way of doing it without the loop?
>
> nleft(length(time))=zeros;
> for i=1:1:length(time)
> nleft(i)=find(tknts(5:nspl+1)<time(i), 1,'last')+4;
> end
>

What is tknts, tknts? Is tknts sorted? If yes you can use HISTC to determine where time is inserted. There is a detail on the comparison operator <= and < that you need to be aware, and can be worked out.

> spl(nsplt,m)=zeros;
> spl(nleft-3:nleft,:)=bspline(tknts,time,jord,nleft);
>
> Here, 'bspline' returns a 4 x m matrix and 'nleft' is still a 1 x m vector. What I want to do is assign the each column of the returned matrix into 'spl' starting at a specific row determined by the value in corresponding column of the vector 'nleft'. The starting rows are all different.
> Currently, all values returned by the function are assigned to the same rows within 'spl', which is not what I want.

Use SPARSE or ACCUMARRAY or BLKDIAGS to build such shifted matrix.

Bruno
From: Greig on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <i2bcrp$481$1(a)fred.mathworks.com>...
> "Greig " <greig(a)abc.com> wrote in message <i2b8cv$k7$1(a)fred.mathworks.com>...
> > I have translated a program from Fortran to MATLAB with the aim of speeding it up using vectorization, but I'm needing a little help to get it running smoothly. It runs OK with scalars, but there are a few issues with vectorization.
> >
> > First, this code snippet works, but is there a way of doing it without the loop?
> >
> > nleft(length(time))=zeros;
> > for i=1:1:length(time)
> > nleft(i)=find(tknts(5:nspl+1)<time(i), 1,'last')+4;
> > end
> >
>
> What is tknts, tknts? Is tknts sorted? If yes you can use HISTC to determine where time is inserted. There is a detail on the comparison operator <= and < that you need to be aware, and can be worked out.
>
> > spl(nsplt,m)=zeros;
> > spl(nleft-3:nleft,:)=bspline(tknts,time,jord,nleft);
> >
> > Here, 'bspline' returns a 4 x m matrix and 'nleft' is still a 1 x m vector. What I want to do is assign the each column of the returned matrix into 'spl' starting at a specific row determined by the value in corresponding column of the vector 'nleft'. The starting rows are all different.
> > Currently, all values returned by the function are assigned to the same rows within 'spl', which is not what I want.
>
> Use SPARSE or ACCUMARRAY or BLKDIAGS to build such shifted matrix.
>
> Bruno

--

tknts is a sorted vector, time isn't. I can see how histc could be used, but I'm not sure how to implement it while keeping the indices of nleft corresponding to those of time. I guess one solution would be to modified input variables and sort everything by time and pick out the values in sequence.

I think ACCUMARRAY is what I want for the second problem. I will have a fiddle with it and see what I can do... but I'll probably be back!

Cheers
G
From: Bruno Luong on
>
> tknts is a sorted vector, time isn't. I can see how histc could be used, but I'm not sure how to implement it while keeping the indices of nleft corresponding to those of time. I guess one solution would be to modified input variables and sort everything by time and pick out the values in sequence.

Not sure what is the problem, try this:

nspl = 9;
tknts = [0 sort(rand(1,nspl+1))];
% Generate randomly time between tknts(5) and tknts(end)
time = tknts(5)+(tknts(end)-tknts(5))*rand(1,20);

nleft=zeros(size(time));
for i=1:1:length(time)
nleft(i)=find(tknts(5:nspl+1)<=time(i), 1,'last')+4;
end
nleft

%%
[trash nleft2] = histc(time, [tknts inf]);
nleft2

% Bruno
From: Greig on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <i2biul$sri$1(a)fred.mathworks.com>...
> >
> > tknts is a sorted vector, time isn't. I can see how histc could be used, but I'm not sure how to implement it while keeping the indices of nleft corresponding to those of time. I guess one solution would be to modified input variables and sort everything by time and pick out the values in sequence.
>
> Not sure what is the problem, try this:
>
> nspl = 9;
> tknts = [0 sort(rand(1,nspl+1))];
> % Generate randomly time between tknts(5) and tknts(end)
> time = tknts(5)+(tknts(end)-tknts(5))*rand(1,20);
>
> nleft=zeros(size(time));
> for i=1:1:length(time)
> nleft(i)=find(tknts(5:nspl+1)<=time(i), 1,'last')+4;
> end
> nleft
>
> %%
> [trash nleft2] = histc(time, [tknts inf]);
> nleft2
>
> % Bruno

---
Ah, indeed it is easy when you know how. I was looking at the count and not the bin number. Thanks again.

G
 | 
Pages: 1
Prev: ginput, MatLAB GUI
Next: PCA...guys urgent help