This question already has answers here:
Reading comment lines correctly in an input file using Fortran 90
(3 answers)
Closed 8 years ago.
I am trying to read a number from a file .dat in Fortran 90. The problem is than this .dat file has lines starting with the "#" symbol which I want Fortran to ignore. Here is the file I am reading
#number of free parameters= 252
#used norm for the fit= 3.231250e+02
#0 0 0 0 0 -6.458147e+06
#0 0 0 0 1 3.105635e+01
#0 0 0 0 2 2.525518e-04
#0 0 0 0 3 -4.281012e-09
#0 0 0 0 4 -2.759312e-14
#0 0 0 0 5 1.534108e-19
#0 0 0 1 0 6.646356e-01
#0 0 0 1 1 7.311006e-04
#0 0 0 1 2 -2.619385e-09
#0 0 0 1 3 -2.686922e-14
#0 0 0 1 4 -1.679757e-20
#0 0 0 2 0 -3.188727e-04
#0 0 0 2 1 -6.512261e-10
#0 0 0 2 2 2.366201e-14
#0 0 0 2 3 -7.321188e-20
#0 0 0 3 0 -2.747463e-09
#0 0 0 3 1 -2.713283e-15
#0 0 0 3 2 6.595914e-20
#0 0 0 4 0 -3.916931e-14
#0 0 0 4 1 -1.166710e-19
#0 0 0 5 0 7.337420e-20
#0 0 1 0 0 1.098464e+02
#0 0 1 0 1 4.974315e-04
#0 0 1 0 2 2.851914e-09
#0 0 1 0 3 -2.829991e-14
#0 0 1 0 4 -1.078132e-19
#0 0 1 1 0 -3.428791e-04
#0 0 1 1 1 2.540000e-09
#0 0 1 1 2 -5.170717e-14
#0 0 1 1 3 9.467328e-20
#0 0 1 2 0 1.536840e-09
#0 0 1 2 1 -2.862305e-14
#0 0 1 2 2 2.214778e-19
#0 0 1 3 0 -7.982451e-15
#0 0 1 3 1 -1.115507e-19
#0 0 1 4 0 -1.784763e-19
#0 0 2 0 0 -3.354193e-04
#0 0 2 0 1 -2.608933e-09
#0 0 2 0 2 1.603134e-14
#0 0 2 0 3 1.544635e-21
#0 0 2 1 0 9.286391e-11
#0 0 2 1 1 2.212860e-14
#0 0 2 1 2 4.500369e-19
#0 0 2 2 0 4.355379e-14
#0 0 2 2 1 -1.988663e-19
#0 0 2 3 0 1.375598e-19
#0 0 3 0 0 -5.089157e-09
#0 0 3 0 1 -6.290206e-15
#0 0 3 0 2 6.371429e-20
#0 0 3 1 0 -1.164189e-14
#0 0 3 1 1 1.980810e-19
#0 0 3 2 0 -1.530186e-20
#0 0 4 0 0 -8.497320e-15
#0 0 4 0 1 -3.419311e-20
#0 0 4 1 0 1.151535e-19
#0 0 5 0 0 -4.965320e-20
#0 1 0 0 0 -1.259427e+02
#0 1 0 0 1 -3.900548e-05
#0 1 0 0 2 3.691997e-09
#0 1 0 0 3 1.526078e-14
#0 1 0 0 4 2.153889e-19
#0 1 0 1 0 -1.518887e-04
#0 1 0 1 1 1.725796e-09
#0 1 0 1 2 -1.001640e-14
#0 1 0 1 3 1.515356e-19
#0 1 0 2 0 2.629831e-09
#0 1 0 2 1 6.313110e-15
#0 1 0 2 2 3.219031e-19
#0 1 0 3 0 2.906599e-14
#0 1 0 3 1 2.167608e-19
#0 1 0 4 0 1.696993e-19
#0 1 1 0 0 -9.267004e-04
#0 1 1 0 1 2.093122e-09
#0 1 1 0 2 4.148898e-14
#0 1 1 0 3 9.015903e-20
#0 1 1 1 0 -5.283470e-09
#0 1 1 1 1 2.942465e-14
#0 1 1 1 2 -1.620549e-19
#0 1 1 2 0 -1.392080e-15
#0 1 1 2 1 4.239008e-21
#0 1 1 3 0 4.896848e-20
#0 1 2 0 0 2.685715e-09
#0 1 2 0 1 1.666833e-14
#0 1 2 0 2 -7.874688e-20
#0 1 2 1 0 9.224232e-15
#0 1 2 1 1 -4.773437e-19
#0 1 2 2 0 -1.519710e-19
#0 1 3 0 0 5.053813e-14
#0 1 3 0 1 4.160181e-20
#0 1 3 1 0 1.370084e-19
#0 1 4 0 0 -8.886168e-21
#0 2 0 0 0 -2.300488e-04
#0 2 0 0 1 7.564571e-09
#0 2 0 0 2 -1.102233e-14
#0 2 0 0 3 -6.403138e-21
#0 2 0 1 0 -4.206376e-09
#0 2 0 1 1 -3.403031e-14
#0 2 0 1 2 -3.346447e-19
#0 2 0 2 0 5.381533e-15
#0 2 0 2 1 -1.823920e-19
#0 2 0 3 0 -1.178995e-19
#0 2 1 0 0 -1.655777e-09
#0 2 1 0 1 2.935844e-14
#0 2 1 0 2 -6.278178e-20
#0 2 1 1 0 1.300195e-14
#0 2 1 1 1 -8.475176e-21
#0 2 1 2 0 1.052778e-19
#0 2 2 0 0 3.096973e-15
#0 2 2 0 1 1.549711e-19
#0 2 2 1 0 -7.566176e-20
#0 2 3 0 0 -2.251950e-19
#0 3 0 0 0 -3.739465e-09
#0 3 0 0 1 2.295125e-14
#0 3 0 0 2 -4.084332e-20
#0 3 0 1 0 4.950778e-15
#0 3 0 1 1 -1.106986e-19
#0 3 0 2 0 -4.027040e-20
#0 3 1 0 0 1.621460e-14
#0 3 1 0 1 -1.062879e-19
#0 3 1 1 0 -2.483709e-19
#0 3 2 0 0 1.690545e-19
#0 4 0 0 0 6.163400e-15
#0 4 0 0 1 6.288739e-20
#0 4 0 1 0 1.192787e-19
#0 4 1 0 0 -1.455520e-19
#0 5 0 0 0 -9.933712e-20
#1 0 0 0 0 -3.666264e+01
#1 0 0 0 1 9.549133e-04
#1 0 0 0 2 -4.365234e-09
#1 0 0 0 3 2.588782e-16
#1 0 0 0 4 -2.076915e-20
#1 0 0 1 0 1.481452e-03
#1 0 0 1 1 1.904617e-09
#1 0 0 1 2 -1.930502e-14
#1 0 0 1 3 2.011388e-20
#1 0 0 2 0 8.142289e-11
#1 0 0 2 1 -2.718983e-14
#1 0 0 2 2 -8.626389e-20
#1 0 0 3 0 1.100715e-14
#1 0 0 3 1 -6.753546e-21
#1 0 0 4 0 1.454479e-19
#1 0 1 0 0 1.163360e-03
#1 0 1 0 1 -1.811381e-09
#1 0 1 0 2 -3.732141e-14
#1 0 1 0 3 -1.731446e-19
#1 0 1 1 0 -1.368255e-09
#1 0 1 1 1 -1.199297e-14
#1 0 1 1 2 -1.829752e-19
#1 0 1 2 0 8.200737e-15
#1 0 1 2 1 -1.118786e-19
#1 0 1 3 0 -9.719783e-20
#1 0 2 0 0 -2.880947e-09
#1 0 2 0 1 1.251979e-14
#1 0 2 0 2 -1.047222e-19
#1 0 2 1 0 -1.228216e-14
#1 0 2 1 1 9.662320e-20
#1 0 2 2 0 -3.024362e-21
#1 0 3 0 0 -2.423240e-14
#1 0 3 0 1 8.306093e-20
#1 0 3 1 0 -1.033559e-19
#1 0 4 0 0 -7.429864e-20
#1 1 0 0 0 6.989398e-04
#1 1 0 0 1 -1.581314e-09
#1 1 0 0 2 -2.193096e-14
#1 1 0 0 3 -1.899676e-19
#1 1 0 1 0 3.866920e-09
#1 1 0 1 1 -5.727360e-15
#1 1 0 1 2 -3.347686e-20
#1 1 0 2 0 -2.266355e-14
#1 1 0 2 1 3.559480e-19
#1 1 0 3 0 3.783552e-20
#1 1 1 0 0 -4.351640e-09
#1 1 1 0 1 -4.493149e-14
#1 1 1 0 2 3.886234e-20
#1 1 1 1 0 1.795029e-14
#1 1 1 1 1 -1.723292e-19
#1 1 1 2 0 3.898557e-19
#1 1 2 0 0 3.297662e-14
#1 1 2 0 1 -6.122045e-20
#1 1 2 1 0 -3.220305e-20
#1 1 3 0 0 1.550060e-19
#1 2 0 0 0 1.132462e-09
#1 2 0 0 1 9.649800e-15
#1 2 0 0 2 -1.487728e-19
#1 2 0 1 0 -1.033199e-14
#1 2 0 1 1 -1.066775e-19
#1 2 0 2 0 -1.074791e-19
#1 2 1 0 0 -5.672266e-14
#1 2 1 0 1 -4.539318e-19
#1 2 1 1 0 4.278144e-20
#1 2 2 0 0 -1.761885e-20
#1 3 0 0 0 3.350312e-14
#1 3 0 0 1 1.410364e-19
#1 3 0 1 0 1.155801e-19
#1 3 1 0 0 1.753253e-19
#1 4 0 0 0 2.302075e-19
#2 0 0 0 0 -1.251751e-03
#2 0 0 0 1 -1.528697e-09
#2 0 0 0 2 2.022063e-14
#2 0 0 0 3 3.612229e-19
#2 0 0 1 0 -1.394598e-09
#2 0 0 1 1 1.493080e-14
#2 0 0 1 2 2.747549e-19
#2 0 0 2 0 1.440434e-14
#2 0 0 2 1 -1.147694e-19
#2 0 0 3 0 5.537588e-21
#2 0 1 0 0 6.623155e-09
#2 0 1 0 1 2.730209e-15
#2 0 1 0 2 4.181196e-20
#2 0 1 1 0 1.488522e-14
#2 0 1 1 1 -1.643706e-20
#2 0 1 2 0 -3.837800e-19
#2 0 2 0 0 -3.120561e-14
#2 0 2 0 1 9.825753e-20
#2 0 2 1 0 1.769783e-19
#2 0 3 0 0 -1.549952e-19
#2 1 0 0 0 -3.557290e-09
#2 1 0 0 1 1.033207e-14
#2 1 0 0 2 -1.516672e-19
#2 1 0 1 0 1.499897e-14
#2 1 0 1 1 1.675754e-20
#2 1 0 2 0 1.290456e-20
#2 1 1 0 0 3.365246e-14
#2 1 1 0 1 1.912043e-19
#2 1 1 1 0 1.251717e-19
#2 1 2 0 0 4.824158e-20
#2 2 0 0 0 -6.775576e-15
#2 2 0 0 1 3.018091e-19
#2 2 0 1 0 -1.955331e-19
#2 2 1 0 0 1.276077e-20
#2 3 0 0 0 2.575621e-20
#3 0 0 0 0 -8.039580e-10
#3 0 0 0 1 -6.643968e-14
#3 0 0 0 2 1.627232e-19
#3 0 0 1 0 -8.617469e-15
#3 0 0 1 1 1.842556e-19
#3 0 0 2 0 -6.082756e-20
#3 0 1 0 0 -2.047656e-15
#3 0 1 0 1 -6.400721e-20
#3 0 1 1 0 -1.705284e-19
#3 0 2 0 0 3.026086e-19
#3 1 0 0 0 4.164057e-14
#3 1 0 0 1 -6.256086e-20
#3 1 0 1 0 -9.961920e-20
#3 1 1 0 0 -2.004688e-19
#3 2 0 0 0 -4.014110e-19
#4 0 0 0 0 1.059104e-14
#4 0 0 0 1 -3.637782e-20
#4 0 0 1 0 -2.760611e-21
#4 0 1 0 0 1.484074e-19
#4 1 0 0 0 4.916317e-20
#5 0 0 0 0 -1.274646e-20
#average insample error= 1.010769e-01
1.374988e+05
I only need to read the value "1.374988e+05", any idea?
This program solves the problem by implementing the suggestion to "skip the lines with empty read or use iostat and use the first succesful read."
program xread_data
implicit none
integer, parameter :: iu = 10
real :: x
integer :: ierr
open (unit=iu,file="data.txt",action="read")
do
read (iu,*,iostat=ierr) x
if (ierr == 0) exit
end do
print*,"x =",x
end program xread_data
Related
To sort a matrix according to all columns except the first, I used the following code. I do not want sortrows to consider the first column because that is meant to keep track of the row numbers.
B = [1 1 0 0 0 0 0 0 0 1
2 0 1 0 0 0 0 1 0 0
3 0 0 1 0 1 0 0 1 0
4 0 1 0 0 0 1 1 0 0
5 0 0 1 0 0 0 0 1 0
6 0 0 0 0 0 1 1 0 0
7 1 0 0 1 0 0 0 0 0
8 0 0 1 0 1 0 0 0 0];
D = -sortrows(-B,[2:size(B,2)])
What if you want to sort the matrix according to all rows except the first, so the first element of each column would be ignored when sorting them in descending order? Is there any similar function to sortrows?
To clarify, the desired output is
1 0 0 0 0 0 0 1 0 1
2 1 1 0 0 0 0 0 0 0
3 0 0 1 1 1 0 0 0 0
4 1 1 0 0 0 1 0 0 0
5 0 0 1 1 0 0 0 0 0
6 1 0 0 0 0 1 0 0 0
7 0 0 0 0 0 0 1 1 0
8 0 0 1 0 1 0 0 0 0
You can do this via
transposing the input and output
keeping column 1 separate
you can use negative sort indices to avoid what you've done making the input and output negative
A = [B(:,1) sortrows( B(:,2:end).', -(2:size(B,1)) ).'];
>> A
A =
1 0 0 0 0 0 0 1 0 1
2 1 1 0 0 0 0 0 0 0
3 0 0 1 1 1 0 0 0 0
4 1 1 0 0 0 1 0 0 0
5 0 0 1 1 0 0 0 0 0
6 1 0 0 0 0 1 0 0 0
7 0 0 0 0 0 0 1 1 0
8 0 0 1 0 1 0 0 0 0
What matlab command, or combination of commands (using 25 characters or less), could be used to create the following matrix?
1 0 0 0 0 1 0 0 0 0
1 1 0 0 0 1 1 0 0 0
1 1 1 0 0 1 1 1 0 0
1 1 1 1 0 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 1 0 0 0 0
1 1 0 0 0 1 1 0 0 0
1 1 1 0 0 1 1 1 0 0
1 1 1 1 0 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 1 0 0 0 0
1 1 0 0 0 1 1 0 0 0
1 1 1 0 0 1 1 1 0 0
1 1 1 1 0 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1
Hint: Look for a lower triangluar matrix that is repeated many times. First try to produce that lower triangular matrix with as few characters as possible.
You can use the following code:
A = ones(5); % create 5x5 matrix with all elements 1
B = tril(A); % return the lower triangle matrix of A
C = repmat(B, 3, 2); % repeat the matrix B, 3 times in a row and 2 times in a cloumn as you want.
In more details:
A = ones(5);
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
B = tril(A);
1 0 0 0 0
1 1 0 0 0
1 1 1 0 0
1 1 1 1 0
1 1 1 1 1
C = repmat(B, 3, 2);
1 0 0 0 0 1 0 0 0 0
1 1 0 0 0 1 1 0 0 0
1 1 1 0 0 1 1 1 0 0
1 1 1 1 0 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 1 0 0 0 0
1 1 0 0 0 1 1 0 0 0
1 1 1 0 0 1 1 1 0 0
1 1 1 1 0 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 1 0 0 0 0
1 1 0 0 0 1 1 0 0 0
1 1 1 0 0 1 1 1 0 0
1 1 1 1 0 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1
A solution using rempat and implicit expansion:
repmat(1:5<(2:6)',3,2)
I'm writing an optimal path planning algorithm for one of my projects, and am a bit stuck on how to generate the map. In order to test my algorithm, I want to generate random maps that the object will pass through with obstacles in the way. To generate the maps I am making a logical array with "1" for wall or obstacle and "0" when there is nothing in the way. I am having some issues placing the obstacles however.
For now, this is what I've written in MATLAB (though I think the core idea can be solved in any language):
% lenx = length in x-direction of map [m]
% leny = length in y-direction of map [m]
% numObs = number of obstacles
% obsLoc = 2D array with the central location(s) of the obstacle(s)
% [x1, y1; x2 y2; ...]
% obsSize = size of the obstacles (all same size) [m]
% assume the obstacles are square.
% res = resolution of the map [cell/m]
%
% Outputs:
% map = 2D logical array which translates into a map
%
%---------------------------------------------------------
function map = createMap(lenx, leny, numObs, obsLoc, obsSize, res)
%Making map of the given size
map = zeros(leny*res, lenx*res);
%Adding walls
map(:,1) = 1;
map(:, lenx*res) = 1;
map(1,:) = 1;
map(leny*res, :) = 1;
%Recalculating obstacle size for this grid
obsSize = obsSize - 1;
%Adding obstacles
for i = 1:1:numObs
map(obsLoc(i,1)*res-obsSize*res:obsLoc(i,1)*res+obsSize*res,...
obsLoc(i,2)*res-obsSize*res:obsLoc(i,2)*res+obsSize*res) = 1;
end
end
The obsSize parameter is representing the dimension of one of the sides of the square obstacle. Here is a sample solution I ran:
>> createMap(10,10,1,[5,5],1,1)
ans =
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 1 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1
Looks good! But the issue begins for obstacle sizes and resolutions that are not 1 or 2...
>> createMap(10,10,1,[5,5],0.5,2)
ans =
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0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 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
For the first one, we would expect there to be a 1 somewhere close to the middle, but it's nowhere to be found. In the second one, we'd expect to find 6 ones in each direction (3m * 3cell/m = 6 cell), but we are getting 8 (It's also possible that this is not what my code is calculating, but this is what I'm hoping it does. Neither case is working, and I think it has to do with this line:
Recalculating obstacle size for this grid
obsSize = obsSize - 1;
I'm pretty sure I do have to recalculate the object size so it fits to the grid, but how can I do this for any grid and obstacle? Is there a way to scale that "-1" such that it acts according to the size of the grid/obstacle?
Let me know what you come up with, and if there's any other glaring issues in my code?
Thanks!
I have an array, data, in MATLAB just like following:
data = [0 0 0 3 3 3 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1;
0 0 0 3 3 3 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1;
0 0 0 3 3 3 3 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1;
0 0 0 3 3 3 3 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1;
0 0 0 3 3 3 3 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1;
0 0 0 3 3 3 3 3 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1;
0 0 0 0 3 3 3 3 3 3 3 3 3 3 0 0 0 0 0 0 0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3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 1 1 1 1;
0 0 0 0 0 0 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 1 1 1];
Numbers in this matrix stand for color. For example:
1 → red
0 → white
it not must be these colors, anyway,
different number → different color
I want to show on a two-dimensional plane.
I use for environment plot points (but these are discrete points, not a region).
If I use surf(data), it will have a 3D plot:
I can rotate this 3D plot and view above, just like:
This image is what I want plot on 2D plane.
Is there any way to plot on a 2D plane directly?
You may want a contour plot. You can get this by:
[X, Y] = meshgrid(1:size(data,2), 1:size(data,1));
contourf(X,Y,data);
For a more square and distinct look you could try:
scatter(reshape(X,[],1), reshape(Y,[],1), 1200, reshape(data,[1],1), 'square', 'filled');
axis equal
However, this solution requires that you to trial-and-error the size (1200) which will be dependent on the screen size of your plot.
Finally, in newer Matlab versions (R2017b+) you may use heatmap for these types of plots (https://se.mathworks.com/help/matlab/ref/heatmap.html) E.g.:
heatmap(data, 'CellLabelColor', 'none')
I have a matrix that contains data of 0 & 1. I want to find groups of ones (not a specific size) in that matrix. Is it possible somehow?
Thanks in advance!
If you mean that you want to find all the "connected components in the matrix, say BW, simply use:
BW = logical([1 1 1 0 0 0 0 0
1 1 1 0 1 1 0 0
1 1 1 0 1 1 0 0
1 1 1 0 0 0 1 0
1 1 1 0 0 0 1 0
1 1 1 0 0 0 1 0
1 1 1 0 0 1 1 0
1 1 1 0 0 0 0 0]);
L = bwlabel(BW,4) %Result
This would yeild:
L =
1 1 1 0 0 0 0 0
1 1 1 0 2 2 0 0
1 1 1 0 2 2 0 0
1 1 1 0 0 0 3 0
1 1 1 0 0 0 3 0
1 1 1 0 0 0 3 0
1 1 1 0 0 3 3 0
1 1 1 0 0 0 0 0
Now if you want to find the size of various groups:
for ii=1:max(L(:))
length_vector(ii)=length(find(L==ii));
end
length_vector
This gives you:
length_vector =
24 4 5