Extend ndgrid() to any homogeneous data type (booleans, integers, polynomials, rationals, strings, []). Patch Attached
@sgougeon)
Reported by Samuel GOUGEON (ndgrid.sci (3.42 KB, text/plain)
ndgrid.sci upgraded
BUG DESCRIPTION:
----------------
ndgrid() works only with decimals, while it is needed as well for other homogeneous data types.
Basically it implements a simple vectors replication scheme.
Such an operation can be done whatever is the data types, and is useful and expected in particular with integers and strings.
An example is given with strings. Many other examples could be found, as well with booleans or mixed types.
ndgrid()'s short description might be updated into:
Builds arrays of orthogonally replicated vectors
// With decimals: OK
-->x = 1:3
x =
1. 2. 3.
-->y = 4:9
y =
4. 5. 6. 7. 8. 9.
-->[X, Y] = ndgrid(x,y)
Y =
4. 5. 6. 7. 8. 9.
4. 5. 6. 7. 8. 9.
4. 5. 6. 7. 8. 9.
X =
1. 1. 1. 1. 1. 1.
2. 2. 2. 2. 2. 2.
3. 3. 3. 3. 3. 3.
// With other data types: NOK
// Example: Encoded integers
-->[X, Y] = ndgrid(int8(x), int8(y))
!--error 10000
ndgrid: Wrong type for argument #1: Real expected.
at line 36 of function ndgrid called by :
[X, Y] = ndgrid(int8(x),int8(y))
// With the Patch
// --------------
// Integers:
-->[X, Y] = ndgrid(int8(x),int8(y))
Y =
4 5 6 7 8 9
4 5 6 7 8 9
4 5 6 7 8 9
4 5 6 7 8 9
4 5 6 7 8 9
X =
1 1 1 1 1 1
2 2 2 2 2 2
3 3 3 3 3 3
4 4 4 4 4 4
5 5 5 5 5 5
// Simple application with texts: Building a set of digrams
-->[c1, c2] = ndgrid(["a" "b" "c"], ["a" "b" "c" "d" "e" "f" "g" "h"]);
-->c1+c2
ans =
!aa ab ac ad ae af ag ah !
!ba bb bc bd be bf bg bh !
!ca cb cc cd ce cf cg ch !
ERROR LOG:
----------
!--error 10000
ndgrid: Wrong type for argument #1: Real expected.
at line 36 of function ndgrid called by :
[X, Y] = ndgrid(int8(x),int8(y))
HOW TO REPRODUCE THE BUG:
-------------------------
x = int8(1:5)
y = int8(4:9)
[X, Y] = ndgrid(x, y)
[c1, c2] = ndgrid(["a" "b" "c"], ["a" "b" "c" "d" "e" "f" "g" "h"])
OTHER INFORMATIONS:
-------------------
Updated help page to come.