select selected numeber of rows from an huge array

Hi,
I need to select 365 (366,:) rows from array of [3652500 x 2] and write a matrix [366 x 10000]. Can somebody help me with that. I am new to MATLAB.
Thanks in advance.

5 Comments

What do you mean by ‘365 (366,:) rows’?
You will have to delete 3.6550e+006 elements from your [3652500x2] matrix to create your [366x10000] matrix. What elements get included? What elements get left out?
hi,
I need to extract elements from row 1 to 366, 367 to 731..and so on and write to a (366 x 10000) matrix. I am not removing any row or columns but its sort of an arranging in a different way. Hope this helps.
3652500 is a multiple of neither 365 nor 366; could you build an example with e.g. a 6x2 input array?
>> (3652500*2)==(366*10000)
ans =
0
>> num2str((3652500*2)/366,'%.5f')
ans =
19959.01639
>>
Don't fit.
doc reshape % but will have to be commensurate sizes
Your first dimension is being reduced by a factor of about 10000, but your second dimension is being increased by a factor of 5000 (10000 / 2). Where is the remaining data to go?

Sign in to comment.

 Accepted Answer

There are a couple of magic numbers in your question. I guess it is the number of days in a year. "366" is leap year.
>> M = cssm;
>> whos M
returns
Name Size Bytes Class Attributes
M 366x10000 29280000 double
where
function M02 = cssm
N01 = 3652500;
N02 = 1e4;
M01 = randi( [1,17], [N01,2] );
M02 = nan( 366, N02 );
x01 = 1;
for jj = 1 : N02
if rem( jj, 4 ) >= 1
x02 = x01 + 365 - 1;
M02(1:365,jj) = M01( x01:x02, 1 );
x01 = x02+1;
else % leap year
x02 = x01 + 366 - 1;
M02(1:366,jj) = M01( x01:x02, 1 );
x01 = x02+1;
end
end
end
.
Comment: I chose to build the new array from the first column of your array. You can easily modify the code to use the second column or both.
.
Answer to comment:
I downloaded 0001.txt, but I still lack information
  • What is the first column of whole numbers? It is not time information.
  • Which of the first and second column shall be included in the result?
  • There is no way to decide which data belongs to leap years.
Try
M02 = cssm;
whos M02
which returns
Name Size Bytes Class Attributes
M02 366x10000 29280000 double
where
function M02 = cssm( varargin )
narginchk( 0, 1 )
if nargin == 1
filespec = varargin{1};
else
filespec = 'h:\m\cssm\0001.txt';
end
fid = fopen( filespec, 'r' );
cac = textscan( fid, '%f%*f%*f%f' );
fclose( fid );
M01 = cat( 2, cac{:} );
M02 = cssm_( M01 );
end
function M02 = cssm_( M01 )
N02 = 1e4;
M02 = nan( 366, N02 );
x01 = 1;
for jj = 1 : N02
if rem( jj, 4 ) >= 1
x02 = x01 + 365 - 1;
M02(1:365,jj) = M01( x01:x02, 2 );
x01 = x02+1;
else % "leap" year
x02 = x01 + 366 - 1;
M02(1:366,jj) = M01( x01:x02, 2 );
x01 = x02+1;
end
end
end

6 Comments

Hi,
I think you sort of got I wanted to, so I have this array x = 3652500 x 2. How can I make use of your code to create 366 x 10000 matrix. After every 4 years, I need to extract 366 rows but for other years 365. But finally my matrix will look like 366 x 10000.
Need to check for the odd year that isn't leap year -- 2000, for one relatively recent...it's the 400-yr exception to the rule.
@Damith, My code does that. Did you run it? The column of ordinary years are padded with a NaN to give 366 rows. The line
if rem( jj, 4 ) >= 1
need to be replaced by
if not( leapyear( yy ) )
The Aeronautic Toolbox contains a function named, leapyear. The File Exchange contains a few similar functions. You didn't provide time info together with the array x.
.
@dpb, The year 2000 is a leap year because it is an exception to the exception
@per isakson,
I tried to run your code but did not understand where to insert my data array. I have attached the link to download 0001.txt file for you to have a look (see below). Basically i am reading a text file (0001.txt) the 1st and 4th column. That's why I said I have an array of 3652500 x 2. I sort of understand your code but don't know how to modify it. (I can read this txt file and I found the MATLAB code for that)
Thanks again.
@Damith, I added to the answer above. Use the debugging tools to analyze the behavior of the new function, cssm
Here are some links on debugging in Matlab
@ per isason,
Thanks. I figuerd it out using ur code. Many thanks again.

Sign in to comment.

More Answers (0)

Categories

Find more on Functions in Help Center and File Exchange

Asked:

on 12 May 2014

Commented:

on 16 May 2014

Community Treasure Hunt

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

Start Hunting!