Mercurial > hg > octave-nkf
comparison src/data.cc @ 5013:1eb9ce5c0152
[project @ 2004-09-21 22:18:07 by jwe]
author | jwe |
---|---|
date | Tue, 21 Sep 2004 22:22:13 +0000 |
parents | fac558699d0f |
children | 11bea7392e69 |
comparison
equal
deleted
inserted
replaced
5012:ed25bed43409 | 5013:1eb9ce5c0152 |
---|---|
1644 @end example\n\ | 1644 @end example\n\ |
1645 \n\ | 1645 \n\ |
1646 @noindent\n\ | 1646 @noindent\n\ |
1647 Note that the total number of elements in the original\n\ | 1647 Note that the total number of elements in the original\n\ |
1648 matrix must match the total number of elements in the new matrix.\n\ | 1648 matrix must match the total number of elements in the new matrix.\n\ |
1649 \n\ | |
1650 A single dimension of the return matrix can be unknown and is flagged\n\ | |
1651 by an empty argument.\n\ | |
1649 @end deftypefn") | 1652 @end deftypefn") |
1650 { | 1653 { |
1651 octave_value retval; | 1654 octave_value retval; |
1652 | 1655 |
1653 int nargin = args.length (); | 1656 int nargin = args.length (); |
1657 if (nargin == 2) | 1660 if (nargin == 2) |
1658 new_size = args(1).int_vector_value (); | 1661 new_size = args(1).int_vector_value (); |
1659 else if (nargin > 2) | 1662 else if (nargin > 2) |
1660 { | 1663 { |
1661 new_size.resize (nargin-1); | 1664 new_size.resize (nargin-1); |
1662 | 1665 int empty_dim = -1; |
1666 | |
1663 for (int i = 1; i < nargin; i++) | 1667 for (int i = 1; i < nargin; i++) |
1664 { | 1668 { |
1665 new_size(i-1) = args(i).int_value (); | 1669 if (args(i).is_empty ()) |
1666 | 1670 if (empty_dim > 0) |
1667 if (error_state) | 1671 { |
1668 break; | 1672 error ("reshape: only a single dimension can be unknown"); |
1673 break; | |
1674 } | |
1675 else | |
1676 { | |
1677 empty_dim = i; | |
1678 new_size(i-1) = 1; | |
1679 } | |
1680 else | |
1681 { | |
1682 new_size(i-1) = args(i).int_value (); | |
1683 | |
1684 if (error_state) | |
1685 break; | |
1686 } | |
1687 } | |
1688 | |
1689 if (! error_state && (empty_dim > 0)) | |
1690 { | |
1691 int nel = 1; | |
1692 for (int i = 0; i < nargin - 1; i++) | |
1693 nel *= new_size(i); | |
1694 | |
1695 if (nel == 0) | |
1696 new_size(empty_dim-1) = 0; | |
1697 else | |
1698 { | |
1699 int size_empty_dim = args(0).numel () / nel; | |
1700 | |
1701 if (args(0).numel () != size_empty_dim * nel) | |
1702 error ("reshape: size is not divisble by the product of known dimensions (= %d)", nel); | |
1703 else | |
1704 new_size(empty_dim-1) = size_empty_dim; | |
1705 } | |
1669 } | 1706 } |
1670 } | 1707 } |
1671 else | 1708 else |
1672 { | 1709 { |
1673 print_usage ("reshape"); | 1710 print_usage ("reshape"); |