Reputation: 37
This is an example of what I want to do:
I have a CSV file:
1,200,3,500...
2,400,4,600...
Data are paired, i.e. there are alternating values x,y,x,y….
I want to take this input and make a 2d matrix where the matrix value is m = x–y. Output should be in CSV format with triples x,y,m,x,y,m,…. Any help is appreciated.
Upvotes: 2
Views: 2982
Reputation: 124543
The solution by @VladimirPerković had the right idea. Let me just fix some minor issues:
%# read CSV file
data = csvread('file.csv');
[r c] = size(data);
%# create output matrix
out = zeros(r,c/2*3);
out(:,1:3:end) = data(:,1:2:end);
out(:,2:3:end) = data(:,2:2:end);
out(:,3:3:end) = data(:,1:2:end) - data(:,2:2:end)
%# save as CSV file
csvwrite('out.csv', out)
The output file created:
1,200,-199,3,500,-497,1,200,-199,3,500,-497
2,400,-398,4,600,-596,2,400,-398,4,600,-596
1,200,-199,3,500,-497,1,200,-199,3,500,-497
2,400,-398,4,600,-596,2,400,-398,4,600,-596
Upvotes: 1
Reputation: 1433
This is not a problem:
data = importdata('file.csv');
x = data(1:2:end); //number in the middle means you take every second sample
y = data(2:2:end);
m = x - y;
Now the output:
output_data = [x;y;m];
output_data = output_data(:);
csvwrite ('output.csv' , output_data);
Upvotes: 0