From: James on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hq1uq7$hi1$1(a)fred.mathworks.com>...
> "James " <cche5398(a)uni.sydney.edu.au> wrote in message <hq1tn9$13s$1(a)fred.mathworks.com>...
> > Anyone?
>
> If your goal is to interpolate, then you have to remove repeated values. John's consolidator is the code for that. http://www.mathworks.com/matlabcentral/fileexchange/?term=consolidator
>
> Or directly do:
>
> [Au I J] = unique(A);
> C = accumarray(J(:), B(:)) ./ accumarray(J(:), 1); % compute the mean
> f = @(x) interp1(Au,C,x);
> f(43)
>
> % Bruno

Thanks a lot, Bruno.
I have almost achieve what i wanted to do. The last bit is to multiply f and g.
Here is the little loop i have:
for i = 180:-1:-180
disp(i)
ra(i) = f(i)*g(i)
end
However, with the above loop, i am getting the following error:
??? Attempted to access ra(0); index must be a positive
integer or logical.
Error in ==> testing at 48
ra(i) = f(i)*g(i)

If i remove ra(i), then the script runs fine.
How do i write a loop that gives me f(i)*g(i) whenever i ask for ra(i)?
From: Steven Lord on

"James " <cche5398(a)uni.sydney.edu.au> wrote in message
news:hq284u$7sq$1(a)fred.mathworks.com...
> "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message
> <hq1uq7$hi1$1(a)fred.mathworks.com>...
>> "James " <cche5398(a)uni.sydney.edu.au> wrote in message
>> <hq1tn9$13s$1(a)fred.mathworks.com>...
>> > Anyone?
>>
>> If your goal is to interpolate, then you have to remove repeated values.
>> John's consolidator is the code for that.
>> http://www.mathworks.com/matlabcentral/fileexchange/?term=consolidator
>>
>> Or directly do:
>>
>> [Au I J] = unique(A);
>> C = accumarray(J(:), B(:)) ./ accumarray(J(:), 1); % compute the mean
>> f = @(x) interp1(Au,C,x);
>> f(43)
>>
>> % Bruno
>
> Thanks a lot, Bruno. I have almost achieve what i wanted to do. The last
> bit is to multiply f and g.
> Here is the little loop i have:
> for i = 180:-1:-180
> disp(i)
> ra(i) = f(i)*g(i)
> end
> However, with the above loop, i am getting the following error:
> ??? Attempted to access ra(0); index must be a positive
> integer or logical.
> Error in ==> testing at 48
> ra(i) = f(i)*g(i)
>
> If i remove ra(i), then the script runs fine. How do i write a loop that
> gives me f(i)*g(i) whenever i ask for ra(i)?

There is no such thing in MATLAB as the 0th element, or the -1st element, or
the -180th element of a matrix.

If you want the kth element of ra to contain the product of the kth element
of f and the kth element of g, use elementwise multiplication.

ra = f.*g;

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


From: James on
> There is no such thing in MATLAB as the 0th element, or the -1st element, or
> the -180th element of a matrix.
>
> If you want the kth element of ra to contain the product of the kth element
> of f and the kth element of g, use elementwise multiplication.
>
> ra = f.*g;
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Hey Steve, I have added the above line to my script, but the same error still shows up.
??? Attempted to access ra(0); index must be a positive
integer or logical.

Error in ==> testing at 48
ra(i) = f(i).*g(i)

I am trying to write a loop that stores ra(i) into an array.
I will give you my whole script just so you know what i am doing
A = [-180; 43; 180; 89; 100; 43; 67; 98; 43; -120; -143; -67];
B = [12; 9; 8; 2; 10; 33; 6; 5; 18; 49; 23; 88;];
D = [A,B]
[Au I J] = unique(A);
C = accumarray(J(:), B(:)) ./ accumarray(J(:), 1);
E = [Au, C]
f = @(x) interp1(Au,C,x);
U = [ 98; 34; -79; -180; 34; -99; -12; 89; 180; 123; -9; 155;]
V = [43; 76; 4; 98; 4; 8; 100; 21; 72; 32; 55; 77;]
Y = [U,V]
[Uu K L] = unique(U);
X = accumarray(L(:), V(:)) ./ accumarray(L(:), 1);
Z = [Uu, X]
g = @(x) interp1(Uu,X,x);
for i = 180:-1:-180
ra(i) = f(i).*g(i)
end
From: someone on
"James " <cche5398(a)uni.sydney.edu.au> wrote in message <hq2b0m$gm2$1(a)fred.mathworks.com>...
> > There is no such thing in MATLAB as the 0th element, or the -1st element, or
> > the -180th element of a matrix.
> >
> > If you want the kth element of ra to contain the product of the kth element
> > of f and the kth element of g, use elementwise multiplication.
> >
> > ra = f.*g;
> >
> > --
> > Steve Lord
> > slord(a)mathworks.com
> > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>
> Hey Steve, I have added the above line to my script, but the same error still shows up.
> ??? Attempted to access ra(0); index must be a positive
> integer or logical.
>
> Error in ==> testing at 48
> ra(i) = f(i).*g(i)
>
> I am trying to write a loop that stores ra(i) into an array.
> I will give you my whole script just so you know what i am doing
> A = [-180; 43; 180; 89; 100; 43; 67; 98; 43; -120; -143; -67];
> B = [12; 9; 8; 2; 10; 33; 6; 5; 18; 49; 23; 88;];
> D = [A,B]
> [Au I J] = unique(A);
> C = accumarray(J(:), B(:)) ./ accumarray(J(:), 1);
> E = [Au, C]
> f = @(x) interp1(Au,C,x);
> U = [ 98; 34; -79; -180; 34; -99; -12; 89; 180; 123; -9; 155;]
> V = [43; 76; 4; 98; 4; 8; 100; 21; 72; 32; 55; 77;]
> Y = [U,V]
> [Uu K L] = unique(U);
> X = accumarray(L(:), V(:)) ./ accumarray(L(:), 1);
> Z = [Uu, X]
> g = @(x) interp1(Uu,X,x);
> for i = 180:-1:-180
> ra(i) = f(i).*g(i)
> end

Explicity, Steven's advice was replace:

for i = 180:-1:-180
ra(i) = f(i)*g(i)
end

with

ra = f.*g

(No need for a for loop.)
Do the indexes of f & g really go from -180 to + 180?
I don't think so.
From: Bruno Luong on
"James " <cche5398(a)uni.sydney.edu.au> wrote in message <hq2b0m$gm2$1(a)fred.mathworks.com>...
>
> for i = 180:-1:-180
> ra(i) = f(i).*g(i)
> end

If you do that, you should (re)read the "Getting Started" doc.

- First: don't confuse between index of an array (must be integer, started from 1, used in RA), and input of a functions (used in F and G).
- If you want to store the values of RA with respect to I, you should store both ARRAYS parametrized by the *common* index, as following:

i = 180:-1:-180;
for ind=1:length(i) % ind will be the common index
ra(ind) = f(i(ind))*g(i(ind));
end

% OR better still, the Matlab-style-vectorize compact code:

i = 180:-1:-180;
ra = f(i).*g(i);

Bruno