how to extract rows has a same value in column 1 and plot until those rows of column 2 and 3?

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.

 Akzeptierte Antwort

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 Kommentare

wow, Thanks alot. you saved alot of time. i want to draw like a 2000 graphs for these unique elements... should i write for loop seperate 1,2,3....2000 lines? is there any other way.

You can write for loop like this,

figure;
hold on;
for i=seperate'
  plot(i{:});
end

This method will generate all 2000 plots one figure. Since some number may occur only once in the first column, therefore plot corresponding to that values will just be a single point. You will have to figure out how to deal with such cases. One possible way is to add markers to the plots like this plot(i{:}, '+-'). Or you might want to add am if size(i{:}, 1) > 1 condition and plot only if the number of rows are greater than 1. For a more compact code, please refer to @Guillaume's answer below.

@Ameer Hamza,thanks alot. But i am getting error vectors are not same length. and also i tried below code. yes, i have to plot 2000 seperate graphs and single graph with different notations like color, linestyle etc.
hi,can i find differentiation and its minimum value without plotting all seperate arrays?.
du = diff(seperate{i}) provides 32*2 double values.
but array has more 150 rows? du = diff(seperate{:}) or
du = diff(seperate{i:end}) gives error or many arguments.
2. can i seperate all arrays and then plot(now all in one table- hard to me to plot every array in that cell)
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
@Ameer Hamza, i am unable to extract the index values and tried with loops, find. kindly help where am i doing wrong. comment

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

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);

2 Kommentare

Thank you very much @Guillaume.
Hello,i am facing similar problem. my code is
for w =1:length(mydata)
%[~,I]= find(mydata{w, 5}(:,2)) = find(mydata{w, 1}(:,1));
for o = 1:length(mydata{w,5})
index = find(mydata{w, 5}(:,2) == mydata{w, 1}(:,1)== o);
mydata{w,7} = index;
end
end
i have a cell which contains max and its index values in each row. Now i want to find max index values in another cell and extract those values( matrix 1*2, here i want to extract only 1st col value only). for ex. [M,I] = [2,295]. 295 index value of its max value in certain row, let say (6,26321) is the extracted index value in another cell. extract value is 6(1st col) similarly all other row cells matrix. Thanks in advance. i tried different ways to get those index values before posting here!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Line Plots finden Sie in Hilfe-Center und File Exchange

Gefragt:

Ram
am 23 Apr. 2018

Kommentiert:

Ram
am 29 Jul. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by