From: James on 12 Apr 2010 11:36 Here is what i have so far: A = [123; 43; 89; 100; 43; 67; 98; 43;]; B = [12; 9; 8; 2; 33; 6; 5; 18;]; C = [A,B] The output is thus, C = 123 12 43 9 89 8 100 2 43 33 67 6 98 5 43 18 I want to write a script that reads through A to see if there are any repeated numbers, and calculates the average B value for any repeated numbers. Then, the script automatically replace every B value with that average value. For example, There are 3x43 which have corresponding B values of 9, 33, and 18. Therefore, the average for 43 would be 20 ((9 + 33 +18)/3). I would love the script to replace 9, 33, and 18 with 20. As a MATLAB newbie, I have no idea where to start. Please help. Any kind of help offered would be greatly appreciated.
From: Matt Fig on 12 Apr 2010 13:13 Depending on the sizes of, and value ranges in A and B, your fastest method might vary. Here is one vectorized method that seems to be pretty quick for larger arrays with larger values. % Data A = [123; 43; 89; 100; 43; 67; 98; 43]; B = [12; 9; 8; 2; 33; 6; 5; 18]; % Engine [As,idx] = sort(A); As = cumsum([1;diff(As)~=0]); C = accumarray(As,B(idx),[],@(x) sum(x)/length(x)); [idx,idx] = sort(idx); C = [A,C(As(idx))];
From: James on 13 Apr 2010 04:15 "Matt Fig" <spamanon(a)yahoo.com> wrote in message <hpvkb1$5gt$1(a)fred.mathworks.com>... > Depending on the sizes of, and value ranges in A and B, your fastest method might vary. Here is one vectorized method that seems to be pretty quick for larger arrays with larger values. > > > % Data > A = [123; 43; 89; 100; 43; 67; 98; 43]; > B = [12; 9; 8; 2; 33; 6; 5; 18]; > > > % Engine > [As,idx] = sort(A); > As = cumsum([1;diff(As)~=0]); > C = accumarray(As,B(idx),[],@(x) sum(x)/length(x)); > [idx,idx] = sort(idx); > C = [A,C(As(idx))]; Hey Matt, It's working 100%. What I'm trying to do now is to use the function you showed the other day. Here is what i have after adding your script and the interp1 function A = [123; 43; 89; 100; 43; 67; 98; 43]; B = [12; 9; 8; 2; 33; 6; 5; 18]; % Engine [As,idx] = sort(A); As = cumsum([1;diff(As)~=0]); C = accumarray(As,B(idx),[],@(x) sum(x)/length(x)); [idx,idx] = sort(idx); C = C(As(idx)); f = @(x) interp1(A,C,x); f(43) This is what i get: ??? Error using ==> interp1 at 261 The values of X should be distinct. Error in ==> @(x)interp1(A,C,x) Why isn't this working? I thought assigning the same B value for repeated A should get the script running.
From: James on 13 Apr 2010 10:05 Anyone?
From: Bruno Luong on 13 Apr 2010 10:24 "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
|
Next
|
Last
Pages: 1 2 3 Prev: display figures in matlab distributed computer server Next: colormap help |