mtex-toolbox / mtex

MTEX is a free Matlab toolbox for quantitative texture analysis. Homepage:
http://mtex-toolbox.github.io/
GNU General Public License v2.0
274 stars 182 forks source link

Inconsistent behavior in vector3d constructor? #2145

Open gsparks3 opened 1 month ago

gsparks3 commented 1 month ago

I noticed some inconsistent behavior in the vector3d constructor. I was considering trying to fix it myself, but I wanted to make sure that I didn't introduce any new bugs in the process ... pondering fixes, I think the case of a 3x3 input matrix might be an issue, since you can't unambiguously define whether the output should be a row or column vector solely from the shape of the input, unlike other N x 3 or 3 x N matrices.

Example of inconsistent behavior:

% Create some row vectors
x = rand(1, 10);
y = rand(1, 10);
z = rand(1, 10);

% Create a vector3d from row vectors - result is a row vector (reasonable)
vector3d(x, y, z)

ans = vector3d
 size: 1 x 10
[...]

% Create a vector3d from column vectors - result is a column vector (reasonable)
vector3d(x', y', z')

ans = vector3d
 size: 10 x 1
[...]

% Create a 3 x N matrix
xyz = [x; y; z;];

% Create a vector3d from 3 x N matrix - result is a 1 x N vector3d (reasonable)
vector3d(xyz)

ans = vector3d
 size: 1 x 10
[...]

% Create a vector3d from N x 3 matrix - unexpected result!
vector3d(xyz')

ans = vector3d
 size: 1 x 3
[...]

Expected result: If a 3 x N matrix produces a 1 x N vector3d, a N x 3 matrix should produce a N x 1 vector3d. However, a 3 x 3 matrix is ambiguous. Possibly best to have that case throw a warning or error.

What MTEX version do you use? MTEX 5.11.2

ralfHielscher commented 1 month ago

Hi,

I agree that the behavior is not desirable. Unfortunately, I also do not oversee whether changing this behavior creates unintended bugs. As a workaround I would recommend to use vector3d.byXYZ(d)' whenever you have a Nx3 data matrix andvector3d(d)` whenever you have a 3xN data matrix.

Ralf.