Index on a matrix
12 views (last 30 days)
Show older comments
Hello,
I have a file with a 1600x8 matrix -data.
Interesting are only the first column (time) data (:, 1) and the second column (amplitude) data (:, 2).
I now have 290 files and I want to end up with 290 speeds. I want MAtlab to be in the first column between 0.00002 and 0.000025, then give me the indexes of the matrix and then output the corresponding values of the second column with the indexes. From the values of the second column he should then determine the maximum value and then output to the index the direct value of the first column (time). I've managed that far (maybe a bit awkward, but it works ^^) Now I want to run through this for all 290 files. So in the end I have the velocity vector. If he accesses the 2nd column, I have to somehow be able to map the value of the file (k). Column 1 is the same in each file (same time window).
% Calculate speed
Array=data(data(:,1)>=0.00002 & data(:,1)<=0.000025);
[index]=find(data(:,1)>=0.00002 & data(:,1)<=0.000025);
% From here he accesses the value of the 2nd column
h=data(k)(index,2); ??
Cmax(k)=max(h)
A=[index,Array,h]
u=find(A(:,3)==Cmax(k))
T(k)=A(u,2)
v(k)=0.1/T(k)
My problem now is that I can not apply an index to a matrix because I already use indexes by accessing the matrix.
For a few problem-solving suggestions, I would be grateful.
3 Comments
dpb
on 6 Jul 2019
Edited: dpb
on 8 Jul 2019
Is the end result to be simply a dataset of 290 max amplitude/time for the interval between 0.000020 and 0.000025?
Won't the file be implied by the position in the array if you process sequentially?
That, of course, raises the question of the file naming convention so that the files are processed in the intended order...
Answers (2)
dpb
on 8 Jul 2019
xlo=0.000020;
xhi=0.000025;
d=dir('*.csv');
nFiles=numel(d); % how many are there?
vt=zeros(nFiles,1); % preallocate output array
for i=1:nFiles
data=importdata(d(i).name,',',10); % read comma-delimited file, skip 10 headerlines
ix=iswithin(data(:,1),xlo,xhi); % select within desired bounds
[mx,imx]=max(data(ix,2)); % find max, location in subset
v=0.1/data(ix(imx),1); % retrieve time, compute v at max location
vt(i,:)=[mx v]; % output array
end
It's late and I'm tired so I may have not parsed your logic quite right, but I believe the above is what you're looking for.
The "trick" is to save the location of the max found but then to use it on the other colum besides the one on which did the lookup. You could also make it simpler by saving all columns instead of just the one; or at least both that need when do the subsetting.
iswithin is my "syntactic sugar" utilty routine
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
end
Put it in your working directory or start a Utils directory of your own and add to the MATLABPATH
0 Comments
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!