how to extract rows has a same value in column 1 and plot until those rows of column 2 and 3?
11 次查看(过去 30 天)
显示 更早的评论
Hi everyone, I have a matrix with repeated values in the 1st column, for example:
A = [
1 15 43;
2 05 64;
2 13 32;
3 11 35;
3 01 20;
3 -15 08;
4 46 742;
4 25 234;
..........;]--- let say, there are 2500 rows and repetition values are 2000.
Using A, I am looking to extract data from the 2nd and 3rd column for each value in the 1st column to output B and plot it. Where repetition occurs for a value in the 1st column, the corresponding values in the 2nd and 3rd column are to be plotted (that means 2000 graphs). until end of rows.
I have attempted to use a combination of find functions, if statements and loops, but without success. Ideally, a successful approach would also be efficient, as the actual dataset is large.
0 个评论
采纳的回答
Ameer Hamza
2018-4-23
You can create a cell array of the separated element as
unique_element = unique(A(:,1));
seperate = cell(numel(unique_element), 1);
for i=1:numel(unique_element)
index = A(:,1)==unique_element(i);
seperate{i} = A(index, 2:3);
end
The separated values are stored in variable seperate which is a cell array. You can access its values as seperate{1}, seperate{2}, ... or loop through the entire array using for loop and do whatever manipulation or visualization you want on them.
6 个评论
Ameer Hamza
2018-4-30
diff() should one reduce a dimension by one. You might be making some other mistake. Also, it is better to start a new question, as it different from current question. You will have a better chance of getting a quick answer if more people see your question
更多回答(1 个)
Guillaume
2018-4-23
If all you want to do is plot the two columns against each other according to the first column, and assuming that the first column is made of integers from 1 to N without any gap (doesn't have to be ordered though), then this is probably the easiest:
figure;
hold on;
splitapply(@plot, A(:, 2), A(:, 3), A(:, 1));
If the first column is not made of integers or has gaps in the integer values, then a findgroup first will work around that:
figure;
hold on;
g = findgroups(A(:, 1));
splitapply(@plot, A(:, 2), A(:, 3), g);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polynomials 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!