I have two CSV files that are not equal sizes. for one of the files,I want to obtain all values for when column two is only equal to one. I already did this, the code for this is:
P = N(N(:,2)==1,:);
Now that I have all the data associated with column 2 when it equals to 1, I want this file to line up with another file. So where ever the second column is = to 1, i want the second file to only show values that are at the same position.
my code for this is:
Val = find(isnan(P));
P(Val) = [];
M(Val) = [];
So there arent any nan values in the data set but this does what I want it to do. P is the values associated with column 2 when it = to 1 and M is the second file.

2 Comments

"there arent any nan values in the data set but this does what I want it to do"
If there aren't any nan values then your code does nothing at all.
I'm not entirely sure what you mean by "line up". Can you clarify with an example?
However, just going by the title of your question if you want to match the times of two different files, then importing them as timetables and then synchronize the two. Possibly, this is done with just 3 lines of code.
isamh
isamh on 11 Mar 2020
Edited: isamh on 11 Mar 2020
so lets say file P is: and M is:
A B ... A B ...
3434 1 343 5
2342 1 344 7
2341 1 233 9
1232 0 435 1 <-- get rid of these
3433 0 347 5 <--
3534 1 345 7
3433 0 452 9 <--
5464 1 765 2
I now want to get rid of all the rows that arent equal to 1 and have that happen to the corresponding file

Sign in to comment.

 Accepted Answer

Guillaume
Guillaume on 11 Mar 2020
In your example, what you're asking is simply:
M(P(:, 2) ~= 1, :) = [] %remove all rows of M for which the corresponding row of P is not 1.
How this translates to your real use case, I'm not too sure, particularly as you say that M and P haven't got the same number of rows.

11 Comments

isamh
isamh on 11 Mar 2020
Just tried it, didnt work.
You're going to have to give us a lot more than it didn't work
Using your example:
>> P = [3434, 1; 2342, 1; 2341, 1; 1232, 0; 3433, 0; 3534, 1; 3433, 0; 5464, 1];
>> M = [343, 5; 344, 7; 233, 9; 435, 1; 347, 5; 345, 7; 452, 9; 765, 2];
>> M(P(:, 2) ~= 1, :) = []
M =
343 5
344 7
233 9
345 7
765 2
Exactly the result you asked for.
It's hard to guess what you really want from the limited explanations you give.
isamh
isamh on 13 Mar 2020
sorry for the delayed response, I have two CSV files that I want to implement his on. would this code work for it?
I'm not entirely sure what you're stuck on. Import the csv files with one of the many import functions, readmatrix, readtable, dlmread, csvread, importdata, etc., then apply the code as appropriate. If that's not enough for you, then please attach an example of the text files.
A longer explanation of what you want to do and what the input files represent would also help.
isamh
isamh on 13 Mar 2020
So I have a file: P that has that was recorded in millaseconds and starting value is 136451ms
the other file: M starts at 0 s and was recorded in seconds. when I filter the File P for the second column only when it equals to 1, i find that the starting time coresponding to the filtered data when column 2 is equal to 1. the time is 166551 ms and to time align that with file M, I subtract 166551 ms with 136451 ms to get a time of 30100 ms which equals 30.1seconds. with that, i know that I need the data from File M starting from 30.1 seconds to the end of the file P filtered when column 2 is equal to 1. that filtered data ends at 1538451 ms and to convert that to seconds, I get a values of 1402 seconds which tells me that the data from 30.1 seconds to 1402 seconds is that data that is what i need. How would i be able to get all the data from 30.1seconds to 1402 seconds?
Can you attach some example files?
Comment by Ahmed Issa moved here:
at the moment no, dont have the files. but the code i created is:
Time_A = M(M(:,1) >= 30.1 & M(:,1) < = 1402);
When I do this, I only get this column. how can i do this and get all 4 columns?
is there a way to do this automatically without doing the time conversion?
isamh
isamh on 13 Mar 2020
figured it out, thanks for the help!
isamh
isamh on 18 Mar 2020
M(P(:, 2) ~= 1, :) = [] this actually works, I just noticed it does what I want but also includes the rows that are extra. File M has about 20000 additional rows compared to file P, how can I only get the rows from file P that align with file M?
What do you mean by 'rows that are extra'?
Again, having a concrete example of what you actually want would help.
isamh
isamh on 18 Mar 2020
you know how this: M(P(:, 2) ~= 1, :) = [] finds every sinlge value equal to 1 in the second column of File P and grabs all that data and matches those corresponding locations in File M. If we only find the values of 1 in the second column in File P, number of rows are 13703. When we include File M, the number of rows is about three times that amount because there is about 20,000 additional rows to that file. How would I be able to ignore all addition rows? I am unable to inlcude the files because I'm not really authorized.
an example:
so lets say file P is: and M is:
A B ... A B ...
3434 1 343 5
2342 1 344 7
2341 1 233 9
1232 0 435 1 <-- get rid of these
3433 0 347 5 <--
3534 1 345 7
3433 0 452 9 <--
5464 1 765 2
343 2 <--
678 4 <--

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Asked:

on 11 Mar 2020

Commented:

on 18 Mar 2020

Community Treasure Hunt

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

Start Hunting!