Matlab Help-changing elements in a matrix

edited July 2010 in Science & Tech
I have a matrix consisting of 20 rows and 100 columns. Each element in the matrix is a number between 1 and 10.

I also have 10 predefined values, x1 to x10.

I want to change each element in the matrix so that 1 changes to the value x1, 2 changes to the value x2 and so on.

Any help would be greatly appreciated.

Comments

  • MarushkaMarushka cambridge, ma Icrontian
    edited July 2010
    Here is some pseudo code. The idea i have is to make a vector out of your predefined x1 through x10 values so they are ordered by index (x1 is in index 1, x2 is in index 2, etc). Call that X. Let Y be your matrix of values between 1 and 10.

    Then if you create a new matrix, say Z and let that be equal to X(Y), it will populate the matrix using the values defined in X by the index numbers from Y.

    X=[x1, x2, x3, x4, x5, x6, x7, x8, x9, x10];
    Z=X(Y);

    Z should be the matrix you want.

    For example.
    x1=10;
    x2=20;
    X = [x1 x2]; % X = [10 20];
    Y = [1 1;2 2; 1 2];

    Z=X(Y);

    This will give
    Z = [10 10; 20 20; 10 20];
  • MAGICMAGIC Doot Doot Furniture City, Michigan Icrontian
    edited July 2010
    Sounds so crazy, it just might work.
  • MarushkaMarushka cambridge, ma Icrontian
    edited July 2010
    shhh dont tell Barret654, but i dont know how to code. i just randomly selected characters from the alphabet and it seemed to parse well, so i posted it :)
  • primesuspectprimesuspect Beepin n' Boopin Detroit, MI Icrontian
    edited July 2010
    Pro tip: Non-coders don't use the word "parse"

    NERD
  • MarushkaMarushka cambridge, ma Icrontian
    edited July 2010
    can i get an achievement for being called a nerd by someone on icrontic? i feel very achieved.

    also i like butts.
  • drasnordrasnor Starship Operator Hawthorne, CA Icrontian
    edited July 2010
    So essentially you want to square each element in your matrix? If I define your 20x100 matrix as 'A' and the desired result as 'Z', then:

    Z = A.*A;
Sign In or Register to comment.