help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Issue with the variable after exiting for loop


From: Nicholas Jankowski
Subject: Re: Issue with the variable after exiting for loop
Date: Thu, 28 May 2020 15:13:55 -0400

On Thu, May 28, 2020 at 2:41 PM GK19 <ganeshrkini19@gmail.com> wrote:
hi there was some issue,

do we need to declare abc_p before the for loop??


you should really be more clear about what 'some issue' is.  if you got an error message, tell us the exact error message.

you shouldn't have to declare abc_p. that loop will assign element by element into abc_p, expanding it each time the size needs to increase for the assignment. this could get very slow for large arrays, as you are creating a copy of abc_p every time it needs to expand the size.  

And it seems that all this series of loops is doing is copying the values of period_arr straight into abc_p, up to the range limits defined by all of the length() statements. You could simply calculate the lengths and then do a direct copy of a subset of period_arr.

Example:


GNU Octave, version 5.2.0
Copyright (C) 2020 John W. Eaton and others.
This is free software; see the source code for copying conditions.
There is ABSOLUTELY NO WARRANTY; not even for MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  For details, type 'warranty'.

Octave was configured for "x86_64-w64-mingw32".

Additional information about Octave is available at https://www.octave.org.

Please contribute if you find this software useful.
For more information, visit https://www.octave.org/get-involved.html

Read https://www.octave.org/bugs.html to learn how to submit bug reports.
For information about changes from previous versions, type 'news'.

>> edit
>> A = rand(4,3,4,3)
A =

ans(:,:,1,1) =

   0.394286   0.983926   0.544054
   0.977536   0.225626   0.465681
   0.202869   0.430088   0.517584
   0.345954   0.069145   0.396835

ans(:,:,2,1) =

   0.596955   0.432200   0.266274
   0.029700   0.441046   0.607404
   0.917933   0.478987   0.814926
   0.349460   0.534178   0.865541

ans(:,:,3,1) =

   0.61806   0.40866   0.53618
   0.19810   0.91036   0.25678
   0.86147   0.41897   0.64443
   0.72617   0.64882   0.61407

ans(:,:,4,1) =

   0.55377   0.15132   0.43838
   0.93128   0.99873   0.52454
   0.88036   0.84050   0.81475
   0.25184   0.85859   0.37180

ans(:,:,1,2) =

   0.738389   0.860580   0.059313
   0.062627   0.037361   0.691717
   0.375035   0.761943   0.061768
   0.714663   0.615794   0.324437

ans(:,:,2,2) =

   0.146872   0.256419   0.368157
   0.395067   0.540513   0.529821
   0.407313   0.677987   0.673027
   0.867863   0.035945   0.887424

ans(:,:,3,2) =

   0.4906284   0.5851605   0.0413747
   0.1563992   0.9161093   0.0191849
   0.5943531   0.1002722   0.0900216
   0.0021832   0.6841489   0.6641479

ans(:,:,4,2) =

   0.7567010   0.1851823   0.9950826
   0.6420206   0.0850503   0.3843941
   0.3032742   0.6092447   0.0098233
   0.7586690   0.8456620   0.4855678

ans(:,:,1,3) =

   0.7764234   0.8102405   0.4518130
   0.6454710   0.9681896   0.0082326
   0.3859367   0.8468543   0.4195545
   0.9851542   0.1570787   0.4728957

ans(:,:,2,3) =

   0.389031   0.088541   0.171505
   0.539026   0.942108   0.772355
   0.471107   0.876273   0.109678
   0.046125   0.323779   0.960741

ans(:,:,3,3) =

   0.119634   0.626957   0.144026
   0.379228   0.094830   0.838730
   0.285164   0.054099   0.615673
   0.625802   0.682348   0.782705

ans(:,:,4,3) =

   0.0221458   0.3374686   0.6494100
   0.6061537   0.2564040   0.4978930
   0.8975514   0.4499792   0.2879290
   0.0039387   0.1477363   0.6945299

>> len1 = 2; len2 = 3; len3 = 2; len4 = 2;

>> B = A(1:len1, 1:len2, 1:len3, 1:len4)
B =

ans(:,:,1,1) =

   0.39429   0.98393   0.54405
   0.97754   0.22563   0.46568

ans(:,:,2,1) =

   0.596955   0.432200   0.266274
   0.029700   0.441046   0.607404

ans(:,:,1,2) =

   0.738389   0.860580   0.059313
   0.062627   0.037361   0.691717

ans(:,:,2,2) =

   0.14687   0.25642   0.36816
   0.39507   0.54051   0.52982
 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]