Creating New Column from Calculation

edited September 2007 in Science & Tech
If I have a matix:

1 12 13
2 42 43
3 22 23
4 32 33

How would I take an average of cols 2 & 3 and put those values in a new column, 4? I can calc the averages but it creates a second matrix.

Desired result:

1 12 13 12.5
2 42 43 42.5
3 22 23 22.5
4 32 33 32.5

If I do a max on col 4 to determine the largest value, how would I also pull off the corresponding id no, id is col 1 (max=42.5 and id is 2)?

thanks

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited September 2007
    let's say your matrix is called A:

    A(:,4) = (A(:,2) + A(:,3))/2

    will put the avg in column 4

    if your id's are always 1-based:

    ind = find(A(:,4)==max(A(:,4)));
    if not:
    tmp = find(A(:,4)==max(A(:,4)));
    ind = A(tmp,1);
  • edited September 2007
    shwaip wrote:
    let's say your matrix is called A:

    A(:,4) = (A(:,2) + A(:,3))/2

    will put the avg in column 4

    if your id's are always 1-based:

    ind = find(A(:,4)==max(A(:,4)));
    if not:
    tmp = find(A(:,4)==max(A(:,4)));
    ind = A(tmp,1);

    Thanks again,I'm don't have matlab available right now so I can't try your code. Will this return the index or the actual value of col 1 for the corresponding max? If column 1 were just random id's would I get 6005 for the max value 42.5? What happens if two averages, col 4, were the same value?

    1002 12 13 12.5
    6005 42 43 42.5
    3000 22 23 22.5
    2454 32 33 32.5

    Thanks
    LD
Sign In or Register to comment.