go_lst_to_matrix.m 803 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. function M = go_lst_to_matrix (L)
  2. ## usage: M = go_lst_to_matrix (L)
  3. ##
  4. ## Converts a list to a matrix
  5. ## Assumes list contains equal-length lists as elements
  6. ## Not in ginsh
  7. ## Copyright (C) 2002 by Peter J. Gawthrop
  8. n = eval(g_nops(L));
  9. m = eval(g_nops(g_op(L,"0")));
  10. M = "";
  11. for i = 1:n
  12. M = sprintf("%s[",M);
  13. m_i = g_op(L,int2str(i-1));
  14. if (eval(g_nops(m_i))!=m)
  15. error("All list elements must be lists of same length");
  16. endif
  17. for j = 1:m
  18. m_ij = g_op(m_i,int2str(j-1));
  19. if j>1
  20. jsep = ", ";
  21. else
  22. jsep = "";
  23. endif
  24. M = sprintf("%s%s%s", M, jsep, m_ij);
  25. endfor
  26. if i<n
  27. isep=", ";
  28. else
  29. isep="";
  30. endif
  31. M = sprintf("%s]%s", M, isep);
  32. endfor
  33. M = sprintf("[%s]",M);
  34. endfunction