How to store multiple y-data of arbitrary length for a given x and plot?

I have a two column data where the each element of the second column have arbitrary length. e.g.
X=[1 2 3 4 5]'
Y= [[1 2 3] [1] [] [1 2 10 4] [1 5 2 0 3]]'
How to store this type of y-data in matlab and plot both X vs Y and Y vs X in a scatter plot? i.e. there should be three points on the graph for X=1, no point for X=0 and so on. Note: There can be no elements in some of the arrays of y.
Thanks

 Réponse acceptée

Raj
Raj le 4 Déc 2019
Modifié(e) : Raj le 4 Déc 2019
X=[1 2 3 4 5]';
Y= {[1 2 3] [1] [] [1 2 10 4] [1 5 2 0 3]}';
for ii=1:numel(X)
scatter((repelem(X(ii),numel(cell2mat(Y(ii))))),cell2mat(Y(ii)))
hold on
end

7 commentaires

Many thanks! This produces the correct X vs Y plot. But simple interchange of X and Y shows some error:
Brace indexing is not supported for variables of this type.
"simple interchange of X and Y" - I don't understand what you mean here. Can you give an example? If required, you can modify the code as a workaround solution. Should not be an issue.
Udvas.C
Udvas.C le 4 Déc 2019
Modifié(e) : Udvas.C le 4 Déc 2019
You can use the same data you posted. What to do if I want to just flip the plot (i.e. it plots Y vs X now; how to plot X vs Y)?
X=[1 2 3 4 5]'
Y= [[1 2 3] [1] [] [1 2 10 4] [1 5 2 0 3]]'
When you say Y vs X plot it means that there should be three points [1 2 3] on the graph for X=1, one point [1] for X=2, no point for X=3 and so on. The code I posted does this.
Can you elaborate what are you expecting when you say X vs Y plot?
Let x and y denote axes. I want the y-value be 1 for x=[1 2 3]; y-value be 2 for x=[1] No point for y-value 3 and so on..
Something like this:
Y=[1 2 3 4 5]';
X= {[1 2 3] [1] [] [1 2 10 4] [1 5 2 0 3]}';
for ii=1:numel(X)
scatter(cell2mat(X(ii)),repelem(Y(ii),numel(cell2mat(X(ii)))))
hold on
end
Yes, it worked. Thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Discrete Data Plots dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by