What does [3, Inf] represents?

10 Ansichten (letzte 30 Tage)
Robert101
Robert101 am 3 Dez. 2020
Kommentiert: Robert101 am 3 Dez. 2020
sizeA = [3 Inf];
Does it mean 3 rows and infinite colums?
In the workspace it shows: sizeA = [3, Inf].
How to interpret it?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 3 Dez. 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 Kommentar
Robert101
Robert101 am 3 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

David Hill
David Hill am 3 Dez. 2020
That is an array/vector with two elements 3 and Inf named sizeA. Workspace would show size as 1x2.
  1 Kommentar
Robert101
Robert101 am 3 Dez. 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?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by