Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
693 views
in Technique[技术] by (71.8m points)

octave - How not to transpose vector between runs?

Background

I stumbled upon a behaviour in octave where one of my vectors is a column vector on odd runs of a programme and a row vector else. I need to understand this extremely dangerous behaviour. The following is my attempt to understand.

Programme

My programme bugtest.m looks as follows:

IPMatrixCH = csvread("BugChn.csv"); %has 2 rows

IPCH = IPMatrixCH(:,5);

%Defining a longer shifted vector starting with NaN
IPCH_sh(1) = NaN;
IPCH_sh(2:3) = IPCH(1:2);
IPCH_sh = transpose(IPCH_sh)

Now, if I run this as first and second command after starting octave, I get:

octave:1> bugtest
IPCH_sh =

      NaN
  -1.1946
  -1.1445

octave:2> bugtest
IPCH_sh =

      NaN  -1.1946  -1.1445

Questions

Is my understanding correct that

vecname(i) = value

by default sets the ith component of a ROW vector? My initial idea was that IPMatrixCH would be defined afresh with every call of the programme. Then IPCH would always be a column vector but

IPCH_sh(1) = NaN;
IPCH_sh(2:3) = IPCH(1:2);

would always fill a row vector. Or is IPCH(1:2); ruining this?

Is the observed behaviour because the file is not read afresh, or because octave remembers the orientation of IPCH_sh from the previous run? Or something entirely different? Is there a way to set the orientation of the vector absolutely and not relatively to before? I think that

clear

at the end of the programme resolves the problem, but I would like to understand why it happens!

Input File

Probably unimportant, but for completeness:

"6937",710,26,3,-1.194589,-3.644845,-2.504086,-2.176433,-1.138847,-0.3499769,-0.1842375,0.4976374,"CHN","China"
"6938",710,27,12,-1.144478,-3.201522,-2.375686,-2.029686,-1.09237,-0.327337,-0.13236,0.4428251,"CHN","China"
question from:https://stackoverflow.com/questions/65924859/how-not-to-transpose-vector-between-runs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you don't clear the variable between runs, then IPCH_sh still persists after the first run and for all subsequent runs. Since you're transposing the vector, it changes orientation each time.

the best way to see this would probably be by removing the semicolon after each line so that it shows the output of each step.

The first time you run it, you create a new vector by setting IPCH_sh(1) = NaN. you assign two values to it, then transpose it. The next time you are just replacing the first element, then replacing the second and third elements, then transposing it again. The IPCH_sh(1) = NaN line does not recreate IPCH_sh anew. it just assigns a new value to the first element.

if you want the results to be the same each time, you should make sure you re-initialize any variables that get assignments and may persist between runs. Since you always start setting the first element to NaN, then appending two more values, you could just make the first assignment IPCH_sh = NaN.

example:

>> IPCH = [1 2 3 4 5; 6 7 8 9 10]  #using a dummy IPCH
IPCH =

    1    2    3    4    5
    6    7    8    9   10

>> IPCH_sh(1) = NaN
IPCH_sh = NaN

>> IPCH_sh(2:3) = IPCH(1:2)
IPCH_sh =

   NaN     1     6

>> IPCH_sh = transpose(IPCH_sh)
IPCH_sh =

   NaN
     1
     6

rerunning:

>> IPCH_sh(1) = NaN
IPCH_sh =

   NaN
     1
     6

>> IPCH_sh(2:3) = IPCH(1:2)
IPCH_sh =

   NaN
     1
     6

>> IPCH_sh = transpose(IPCH_sh)
IPCH_sh =

   NaN     1     6

if instead you redefine the variable instead of just making a first element assignment:

>>  IPCH_sh = NaN
IPCH_sh = NaN
>> IPCH_sh(2:3) = IPCH(1:2)
IPCH_sh =

   NaN     1     6

>> IPCH_sh = transpose(IPCH_sh)
IPCH_sh =

   NaN
     1
     6

and again:

>>  IPCH_sh = NaN
IPCH_sh = NaN
>> IPCH_sh(2:3) = IPCH(1:2)
IPCH_sh =

   NaN     1     6

>> IPCH_sh = transpose(IPCH_sh)
IPCH_sh =

   NaN
     1
     6

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...