| How can I get a value from a TI-Basic matrix element?

Previous | TI-Basic and C | Next

Q: | I have a matrix on the top of the expression stack which is produced as a result of calculation, and I don't know how do I put a matrix element located at [i,j] into result (values of 'i' and 'j' may vary)? I have read infos about estack.h, but I couldn't find the answer...
A: | There is a lot of methods. I suggest the following one:
    ESI ptr;
    int result;
    int i = 1;                             // Just an example
    int j = 2;
    push_parse_text ("[[11,12][21,22]]");  // An example matrix
    ptr = locate_element (i,j);
    result = GetIntArg (ptr);              // (assumed that elements are ints)
where `'locate_element'` is an user-written function, which may be implemented as follows:
    ESI locate_element (short m, short n)
    {
      short i;
      ESI ptr = top_estack-1;
      for (i = 0; i < m-1; i++) ptr = next_expression_index (ptr);
      ptr--;
      for (i = 0; i < n-1; i++) ptr = next_expression_index (ptr);
      return ptr;
    }
You can use it as-is, but it will be much better if you can understand how it works.