Index on a matrix

12 views (last 30 days)
Christin Butzlaff
Christin Butzlaff on 6 Jul 2019
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
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...
Christin Butzlaff
Christin Butzlaff on 7 Jul 2019
Hello,
yes the goal would be a recording with 2 columns once amplitude and associated time. The files are read in as follows: (The files themselves are named from 001 to 290)
for k = 1:290 %Gesamtzahl der Dateien
% File Lesen
filename = sprintf('%03d.CSV',k);
if exist(filename, 'file')
S = fileread(filename);
NLpos = find(S == newline);
S(1:NLpos(10)) = []; %delete first 10 lines
S(S==',') = '.';
fmt = repmat('%f', 1, 8);
data = cell2mat( textscan(S, fmt, 'Delimiter', ';'));
else
fprintf('Die Datei %s existiert nicht.\n', filename)
end
end
every data is a 16000x8
Array=data(data(:,1)>=0.00002 & data(:,1)<=0.000025);%Set the time range in the first column
[index]=find(data(:,1)>=0.00002 & data(:,1)<=0.000025);%index of the times found (between 1 and 16000)
h=data(index,2); %associated amplitude to found index
Cmax(k)=max(h) %Maximum value of found amplitudes
A=[index,Array,h]
u=find(A(:,3)==Cmax(k)) %position of found amplitude (in matrix A, not in data)
T(k)=A(u,2) %associated time to maximum amplitude (with A not with data)
v(k)=0.1/T(k)
I grow up like that
h(k) = data (k) (index, 2);

Sign in to comment.

Answers (2)

dpb
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

Christin Butzlaff
Christin Butzlaff on 9 Jul 2019
Many thanks for the answer.
With their code above, I have a data of 10x1 and these are only the first 10 headliner that should be deleted.
In the end, I get a vt with only zero entries. I have now tried with several attempts to integrate their code into mine, which again and again only 290 same speeds. Save the columns individually I believe would have been my next path.
However, this method of speeding is no longer relevant to my work^^, so I will not invest in the solution any more time.
I really appreciate your commitment.

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!