MATLAB Answers

What does [3, Inf] represents?

25 views (last 30 days)
sizeA = [3 Inf];
Does it mean 3 rows and infinite colums?
In the workspace it shows: sizeA = [3, Inf].
How to interpret it?

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 3 Dec 2020
In addition to what the others say: that kind of variable would tend to be used as the size parameter for reading data, such as fread() or fscanf(), both of which literally use the name sizeA in the documentation
In both cases, using [3 inf] as in the sizeA position of fread() or fscanf() would mean that you want to read the data into 3 rows and as many columns as there is available data.
Also (and this is important), the data that is read in is assigned "down" the rows fastest, so the first data item read in would go to (1,1), the second data item read in would go to (2,1), the third to (3,1), the fourth to (1,2), the fifth to (2,2), the sixth to (3,2) and so on. It would be common to be reading from a text file that contained three columns and an unknown number of rows, but you would specify [3 inf] as the size, and the data would be stored down rows instead of across columns. If you fscanf(fid, '%f %f %f', [3 inf]) then the second item of the first row does not get stored to the second column of the first row of the output variable: it gets stored as the second row of the first column of the output.
So, when you are reading a text file that has columns of numbers, the data is read following across rows, but stored down columns. You would typically follow the fscanf() by a transpose to get the data back in the original order
data = fscanf(fid, '%f %f %f', [3 inf]).' ; %notice the transpose
and that transposed data would have the data stored in the same order it appears in the file.
This is a notable difference between computer languages, whether adjacent values stored in memory are adjacent rows in the same column, or adjacent columns in the same row. MATLAB uses "row major order", as does Fortran. C and C++ use "column major order", which is probably more common in computer languages these days.
"row major" compared to "column major" order is not right or wrong for a computer language to use, just a difference. There are two opposing ways that "Western Civilization" humans deal with data, and we tend to mentally flip between them without noticing and then get confused when we have to deal with the consequences either way.
  1. Column major order (like C), adjacent elements are adjacent columns. This corresponds to the convention for graphing, where the first coordinate of an (x,y) pair is the horizontal distance and the second coordinate is the vertical distance. Variable(3,5) corresponding to Variable indexed at x = 3 and y = 5 is a natural match for this arrangement
  2. Row major order (like MATLAB and Fortran), adjacent elements are adjacent columns. This corresponds to the convention for tables of data such as the old log tables like those shown at http://www.sliderules.info/a-to-z/log-4f.gif where you go to a particular row as the less refined coordinate, and then go across the columns for the more detailed answer. You do not say "Go to column 5, line 7", you say "Go to line 7, column 5"
These two conventions are incompatible, so no matter which one the computer language chooses, it will feel "unnatural" when dealing with data that follows the other convention.

  1 Comment

Robert101
Robert101 on 3 Dec 2020
It is still so confusing. I am trying to get my head around. Looks like data loading and reading is itself a big thing. Thanks for your comprehensive reply. I really appreciate it.

Sign in to comment.

More Answers (2)

VBBV
VBBV on 3 Dec 2020
Edited: VBBV on 3 Dec 2020
sizeA is varable with two element vector , 3 and Inf (infinite)
It is NOT a matrix with 3 rows and infinite columns

  3 Comments

Robert101
Robert101 on 3 Dec 2020
fileID = fopen(filename,'r');
formatSpec = '%f,%f,%f';
sizeA = [3 Inf];
A = fscanf(fileID,formatSpec,sizeA);
fclose(fileID);
This is the code fragment. I still do not understand how to interpret it?
Walter Roberson
Walter Roberson on 3 Dec 2020
The interpretation would be that the file has three columns of numbers, separated by commas, but because of the way that MATLAB stores variables in memory, you ask MATLAB to store them in three rows
A, B, C
D, E, F
G, H, I
first item read is A, second item read is B, third item read is C. MATLAB naturally stores those adjacent in memory,
offset 0: A
offset 1: B
offset 2: C
then it pulls in the next line, reading D, then E, then F, and stores those as well in adjacent locations in memory:
offset 0: A
offset 1: B
offset 2: C
offset 3: D
offset 4: E
offset 5: F
this is the order that matlab would use if you did not give a sizeA option -- it would keep reading the items across the lines, and as each item was read, it would add it to the the end of the linear list of items being stored.
Having done that reading into linear memory, you want to create groups of 3, reflecting the original 3 columns, wanting to create a 2D array. The operator to create 2D arrays out of linear memory is reshape(), which needs to be told how many consecutive items to group together, which is 3 in this case. reshape() does not change the order of elements in memory, it just relabels the size() information. So
reshape(vector, 3, numel(vector)/3)
%or
reshape(vector, 3, [])
where the [] means "as many as needed". This would relabel the linear memory to
A D G
B E H
C F I
Because MATLAB stores "down" rows, the order in memory has not changed, this is still the offsets described above, D is still offset 3 relative to the beginning of the array.
But what you wanted instead was
A B C
D E F
G H I
MATLAB stores "down" rows, so the adjacent memory entries for that would be
offset 0: A
offset 1: D
offset 2: G
offset 3: B
offset 4: E
offset 5: H
offset 6: C
offset 7: F
offset 8: I
which is not what you got when the values were stored in linear memory as they were read in. But fortunately it is not difficult to switch order in MATLAB:
reshape(vector, number_of_columns_to_make, []) .' % .' is important
The .' operator (transpose) does reorder in memory (except in the case where you were dealing with a vector) and will move around the entries in the
A D G
B E H
C F I
matrix you got from the reshape(), in order to give you the memory order needed for MATLAB to store
A B C
D E F
G H I
It is completely valid to ask "Well, why doesn't MATLAB store adjacent columns in memory instead of adjacent rows, so that you could just reshape(vector, [], 3) and get out the A B C order after reading in the data?
The answer to that question is that the designers of a computer language must decide which order to use, and that as I explained earlier, no matter which order they use, it is going to conflict with one of the common ways that people use 2D arrays. People want the first index to refer to rows (vertical dimension) when they are dealing with "tables" of numbers, "line 5, column 7", but they want the first index to refer to columns (horizontal dimension) when they are dealing with graphics-like processing, "x = 5, y = 7". Designers have to pick one arrangement, and someone is going to be unhappy no matter what.
Even if you were to adopt graphics order, x = 5, y = 7, you are faced with the question, "Okay, so the next item in linear memory: should it be for the same x and the next y, x =5, y = 8? Or should it be for the same y and the next x, x = 6, y = 7? There is no "right" answer.
Robert101
Robert101 on 3 Dec 2020
Thanks. It is really a good explanation and helps a lot.

Sign in to comment.


David Hill
David Hill on 3 Dec 2020
That is an array/vector with two elements 3 and Inf named sizeA. Workspace would show size as 1x2.

  1 Comment

Robert101
Robert101 on 3 Dec 2020
fileID = fopen(filename,'r');
formatSpec = '%f,%f,%f';
sizeA = [3 Inf];
A = fscanf(fileID,formatSpec,sizeA);
fclose(fileID);
This is the code fragment. I still do not understand how to interpret it?

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by