is it possible to use a function in a vectorization

3 views (last 30 days)
is it at all possible to use function like sum, mean, or sqrt within a vectorization? for example
output(:,3) = sum([ output(:,1) output(:,2) ])
I know that in this case it is simple to just add the two columns without using sum, but I want to use several functions in a more complicated computation, however I can't even get this to work.

Accepted Answer

Jan
Jan on 31 Oct 2011
The colon operator is not defined for vectors. Therefore constantError(:,1)*16-15:constantError(:,1)*16 does not create the expected results. But I'm surprised, that it does not create an error.
a = constantError(:,1)*16-15; % [1; 17; 33; 49; 65]
b = constantError(:,1)*16; % [16; 32; 48; 64; 80]
a:b % 1:16
This is the same as a(1):b(1).
But the problem can be solved easier and faster:
sum(reshape(output(:, 1), 16, []), 1) / 16
In addition I'm using sum()/n directly, which is faster than mean.
  1 Comment
Andrei Bobrov
Andrei Bobrov on 31 Oct 2011
n= 5
constantError = [(1:n)', mean(reshape(output,[],numel(output)/n),2)];

Sign in to comment.

More Answers (5)

Jan
Jan on 31 Oct 2011
As you can see by running your example, it is possible. Even more, it is the idea behind vectorization to use function inside the vectorization.
If you fail with a complicated computation, post it here together with the occurring error messages.
Note: Your example looks strange. If output is a nx3 matrix, the sum will reply a 1x2 vector, But this is unlikely to match into output(:, 3). Posting real examples is usually better.
  2 Comments
jelle
jelle on 31 Oct 2011
I can get a simple mean or sum to work now. however what I am trying to do is calculate a constant error
constantError(:,5) = mean(output(constantError(:,1)*16-15:constantError(:,1)*16,9),2)
??? Subscripted assignment dimension mismatch.
this is not the complete computation yet but it is as far as I am. what I am trying to accomplish is to get the mean of 16 rows from output column 9 to be placed as a single value in the 5th column of constantError. the variable output is 16 times larger than constantError so there are 16 values available for every row of constantError
Jan
Jan on 31 Oct 2011
Is constantVector a [1 x N] row vector?
It is hard to guess such details, and if my guessing is wrong, I'm not helping but confusing you. Please post an example of the code, which inlcude all relevant parts (but not more) and which runs except for the line causing troubles.

Sign in to comment.


jelle
jelle on 31 Oct 2011
the constantVector was a column vector, I changed the dimension of the mean. it now runs, but every mean value is the same. they all are the mean value of the first 16 values of output, where I want the mean of the 1-16, 17-32 etc. the below code is what i use, it runs and contains all the information needed.
constantError = zeros(5,2);
constantError(:,1) = [1;2;3;4;5];
output = [0.501328089500804;0.504524998053765;0.501327312366240;0.505180640182241;0.500650548516782;0.498754590990286;0.496285047697641;0.504107318247037;0.492096825321352;0.505192334685833;0.501946863493327;0.501314406793466;0.500776671646455;0.502551449506760;0.502734124449146;0.496312438477096;0.288229576463202;0.498803122863328;0.294634592548245;0.488895749128325;0.298446317599859;0.499690181189189;0.295325006152020;0.498322252023267;0.282199813206604;0.496495051047178;0.298119326617693;0.498324862873215;0.286597258825539;0.493178344085334;0.279432402259494;0.498659197536277;0.803523379158402;0.504634989265951;0.802180172157025;0.494466409194101;0.800891630161239;0.498629120114384;0.804389791612312;0.498928568755622;0.801034977860838;0.492341059623332;0.793612469721755;0.508663599657952;0.799695779903639;0.503053587683833;0.807459291051300;0.504458450352588;0.284621251952810;0.513318103305132;0.296218665250423;0.509314716101913;0.281257955299990;0.494432007813826;0.286765961448620;0.515846362827202;0.283133092738400;0.518068328043494;0.308954764360037;0.490317210447673;0.298117177924259;0.493907543698726;0.305357407546079;0.491537481975955;0.200522328020183;0.489659374842837;0.195861529557347;0.496753799834638;0.200884517647058;0.501449691364679;0.198921414141491;0.491184925861032;0.202376609333523;0.495315025126173;0.191797848107702;0.505074418146918;0.187775420892031;0.487185926613532;0.200575267572039;0.503403949636894;];
constantError(:,2) = mean(output(constantError(:,1)*16-15:constantError(:,1)*16,1),1);

jelle
jelle on 31 Oct 2011
thanks already for the answers. the sum of output is working perfectly. now I have been trying to substract a value from this mean. this value differs for different rows in constantError based on the value in a second column of constantError. so now constantError contains two columns and the third has to be computed. with the value in the 2nd column I need to search for a value in a third matrix called targetLocation. If constantError(1,2) == 90, than I need the value from the second column of targetLocation from the row that contains 90 in the first column. I hope this is not too confusing.
targetLocation = [0, 0.80, 0.03
45, 0.50+0.3*cos(pi/4), 0.03+0.3*sin(pi/4)
90, 0.50, .33
135, 0.50-0.3*cos(pi/4), 0.03+0.3*sin(pi/4)
180, 0.20, 0.03];
constantError = zeros(5,2);
constantError(:,1) = [1;2;3;4;5];
constantError(:,2) = [90; 135; 0; 135; 180];
output = [0.501328089500804;0.504524998053765;0.501327312366240;0.505180640182241;0.500650548516782;0.498754590990286;0.496285047697641;0.504107318247037;0.492096825321352;0.505192334685833;0.501946863493327;0.501314406793466;0.500776671646455;0.502551449506760;0.502734124449146;0.496312438477096;0.288229576463202;0.498803122863328;0.294634592548245;0.488895749128325;0.298446317599859;0.499690181189189;0.295325006152020;0.498322252023267;0.282199813206604;0.496495051047178;0.298119326617693;0.498324862873215;0.286597258825539;0.493178344085334;0.279432402259494;0.498659197536277;0.803523379158402;0.504634989265951;0.802180172157025;0.494466409194101;0.800891630161239;0.498629120114384;0.804389791612312;0.498928568755622;0.801034977860838;0.492341059623332;0.793612469721755;0.508663599657952;0.799695779903639;0.503053587683833;0.807459291051300;0.504458450352588;0.284621251952810;0.513318103305132;0.296218665250423;0.509314716101913;0.281257955299990;0.494432007813826;0.286765961448620;0.515846362827202;0.283133092738400;0.518068328043494;0.308954764360037;0.490317210447673;0.298117177924259;0.493907543698726;0.305357407546079;0.491537481975955;0.200522328020183;0.489659374842837;0.195861529557347;0.496753799834638;0.200884517647058;0.501449691364679;0.198921414141491;0.491184925861032;0.202376609333523;0.495315025126173;0.191797848107702;0.505074418146918;0.187775420892031;0.487185926613532;0.200575267572039;0.503403949636894;];
constantError(:,3) = (sum(reshape(output(:, 1), 16, []), 1) / 16) - targetLocation((targetLocation == constantError(:,2)),2);
??? Error using ==> eq Matrix dimensions must agree.
Error in ==> matlabExp at 14 constantError(:,3) = (sum(reshape(output(:, 1), 16, []), 1) / 16) - targetLocation((targetLocation == constantError(:,2)),2);
it seems to me that it is somewhat the same problem as before. I use a vector (constantError(:,2)) to search in targetLocation, but I have no idea how to solve it.
  1 Comment
Jan
Jan on 31 Oct 2011
targetLocation has the size 5x3, constantError(:,2) has the size 5x1. You cannot compare them directly using ==. Perhaps you want bsxfun(@eq, a, b).

Sign in to comment.


jelle
jelle on 1 Nov 2011
there was an unlucky coincidence in my code, output is actually 4480 rows and constantError 280 rows, but for the sake of length of the post I reduced this to 80 rows for output and 5 rows for constantError. so even if I specify a column in the comparison between constantError and targetLocation the dimensions are still not the same. what I am trying to achieve is that every value in the 2nd row of constantError is compared to all 5 values in the 1st column of targetLocation. if for example the value of constantError(1,2) is equal to targetLocation(3,1), I need the value that is in targetLocation(3,2). so, the same row but a different column
adjusted code:
targetLocation = [0, 0.80, 0.03
45, 0.50+0.3*cos(pi/4), 0.03+0.3*sin(pi/4)
90, 0.50, .33
135, 0.50-0.3*cos(pi/4), 0.03+0.3*sin(pi/4)
180, 0.20, 0.03];
constantError = zeros(6,2);
constantError(:,1) = [1;2;3;4;5;6];
constantError(:,2) = [90; 135; 0; 135; 180; 45];
output = [0.501328089500804;0.504524998053765;0.501327312366240;0.505180640182241;0.500650548516782;0.498754590990286;0.496285047697641;0.504107318247037;0.492096825321352;0.505192334685833;0.501946863493327;0.501314406793466;0.500776671646455;0.502551449506760;0.502734124449146;0.496312438477096;0.288229576463202;0.498803122863328;0.294634592548245;0.488895749128325;0.298446317599859;0.499690181189189;0.295325006152020;0.498322252023267;0.282199813206604;0.496495051047178;0.298119326617693;0.498324862873215;0.286597258825539;0.493178344085334;0.279432402259494;0.498659197536277;0.803523379158402;0.504634989265951;0.802180172157025;0.494466409194101;0.800891630161239;0.498629120114384;0.804389791612312;0.498928568755622;0.801034977860838;0.492341059623332;0.793612469721755;0.508663599657952;0.799695779903639;0.503053587683833;0.807459291051300;0.504458450352588;0.284621251952810;0.513318103305132;0.296218665250423;0.509314716101913;0.281257955299990;0.494432007813826;0.286765961448620;0.515846362827202;0.283133092738400;0.518068328043494;0.308954764360037;0.490317210447673;0.298117177924259;0.493907543698726;0.305357407546079;0.491537481975955;0.200522328020183;0.489659374842837;0.195861529557347;0.496753799834638;0.200884517647058;0.501449691364679;0.198921414141491;0.491184925861032;0.202376609333523;0.495315025126173;0.191797848107702;0.505074418146918;0.187775420892031;0.487185926613532;0.200575267572039;0.503403949636894;0.490720213124047;0.498207621151426;0.495765606895974;0.487012694404853;0.508015332761726;0.502302546066006;0.497392622769847;0.503514184236409;0.508484481550041;0.502653323943844;0.508301330090239;0.504191865562965;0.499935962953192;0.498087516181503;0.496680407343213;0.497304816702118;];
constantError(:,3) = (sum(reshape(output(:, 1), 16, []), 1) / 16) - targetLocation(bsxfun(@eq,targetLocation(:,1),constantError(:,2)),2);

jelle
jelle on 1 Nov 2011
I solved the second part of my problem. it is not a generic solution but it works for my specific situation.
constantError(:,3) = sqrt(((sum(reshape(output(:,1),16,[])) ./ 16) - targetLocation((constantError(:,2)+45)/45,2)').^2);
I realized that there was a structure in the values I was trying to find in targetLocation. they are all 45 apart, so by adding 45 to the value and then dividing it by 45 I get the number of the row I need. this is fine in this specific situation. but if there is no such way to calculate the row that you need in a different situation, you still need some way to find the correct row. is there a more generic way to solve this?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!