2

Suppose I have a list of people, and I want to store their names in a struct array:

people(1).name = "john"
people(1).lastname = "doe" 
people(2).name = "jane"
people(2).lastname = "smith" 

This will give a "1×2 struct array" with fields name and lastname.

Now, if I do this instead:

peoplev2(1,1).name = "john"
peoplev2(1,1).lastname = "doe" 
peoplev2(2,1).name = "jane"
peoplev2(2,1).lastname = "smith" 

It will give me a "2×1 struct array" with fields name and lastname.

Now, both struct arrays can be accessed in the same way with the same results, despite having different dimensions (one is 1x2 and the other is 2x1):

people(1)

is a struct with name "john" and lastname "doe"

peoplev2(1)

is also a struct with name "john" and lastname "doe".

I would have expected one array to be pivoted respect to the other (1x2 versus 2x1) but they seem equal despite dimension differences. Also, if you visualize them in MATLAB Workspace, they both render as a table with rows 1 and 2, and columns "name" and "lastname" (I thought I would get columns 1 and 2, and rows name and lastname).

enter image description here

How is it that 1x2 versus 2x1 makes no difference at all? In other languages, if you try to iterate a matrix with the typical indexes i and j, in one case i would loop up to 2, and in the other it would be j, they will definitely not be equal.

What am I missing here? Thanks!

jotadepicas
  • 2,389
  • 2
  • 26
  • 48
  • 2
    Look specifically at linear indexing in the duplicate question. You are using linear indexing here, which means that the orientation of the vector doesn't matter. – Cris Luengo May 13 '21 at 22:05

1 Answers1

0

Matrices in MATLAB behave the same way they do in mathematics: [1 2] * [1; 2] = 11 + 22 = 5 (a scalar result) [1; 2] * [1 2] = [1 2 ; 2 4] (a matrix result) So no, a 1x2 is not equivalent to a 2x1.