MATLAB Answers

Convering a table with multiple y values for a single x value

1 view (last 30 days)
Thilakasekaram Varjith
Thilakasekaram Varjith on 28 Oct 2020
Edited: Sourabh Kondapaka on 6 Nov 2020
Hi,
I have a csv file. There is more than 36000 datapoints for both x and y . where multiple x cells have same value for different y values. I want to converge it like for different x values of x columns, y column becomes a vector. can anyone give me a code

  0 Comments

Sign in to comment.

Answers (1)

Sourabh Kondapaka
Sourabh Kondapaka on 6 Nov 2020
Edited: Sourabh Kondapaka on 6 Nov 2020
This is my approach:
  1. Find unique values in the x-axis
  2. For each of the unique value extract corresponding set of values in y-axis.
  3. As there could be varied number of y-axis values for a unique value in x-axis, I'm converting the results into a cell array. Looking at the screenshot, For x-axis value of "1.10E-05" we have 19 values and for 1.11E-05 we have 8 values. So converting the results to cell array would allow us to have variable number of columns.
% I'm using an 100x2 matrix as I do not have your dataset.
% I would suggest setting up breakpoints and looking at how the matrices
% 'a', 'b' and 'results' are being changed.
a = zeros(100,2);
a(:,2) = randn(100,1);
% After executing the below for-loop, the first column will have
% values ranging from 0 to 9.
for i = 1:100
a(i,1) = mod(i,10);
end
b = array2table(a);
% I'm changing this so as to reflect a similar pattern as in your dataset
% as discussed in 3rd point of my approach:
b{10,1} = 19;
%Extracting unique values.
unique_values = unique(b{:,1});
% Converting to cell array so as to allow variable number of columns.
results = cell(length(unique_values), 2);
for i=1:length(unique_values)
results{i, 1} = unique_values(i);
results{i, 2} = table2array(b(b.a1 == unique_values(i), 2))';
end

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by