Assigning matrix rows to the same value and plotting

Hi! So I'm relatively new to coding, so I'm sure there is a simple answer for this. Anyways, here is my problem:
I have a matrix, say x = [1 2 3;4 5 6;7 8 9], and each row corresponds to a different value, say u = -3,0,3. I'm trying to generate a plot like (-3, [1 2 3]), (0,[4 5 6]), (3, [7 8 9]). But I'm having some trouble.
My idea was to generate a matrix of two columns using the following code:
plot_data = [];
for i=1:length(u)
for m = 1:length(x)
x1 = u(i)
y1 = x(i,m)
plot_data(m ,1) = x1
plot_data(m ,2) = y1
end
end
I'm sure I'm doing this in a very roundabout way, and the code I wrote doesn't work correctly. Can anyone give me some help?

1 Kommentar

I don't know what "a plot like (-3, [1 2 3]), (0,[4 5 6]), (3, [7 8 9])" looks like. Mock up something in Photoshop, or draw something and take a photo or it or scan it in, and post a picture of it.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 17 Feb. 2017
x = [1 2 3;4 5 6;7 8 9];
u = [-3,0,3];
y = repmat(u(:), 1, size(x,2));
plot(x.', y.')
ylim([min(u)-1, max(u)+1])

Weitere Antworten (0)

Gefragt:

am 17 Feb. 2017

Kommentiert:

am 17 Feb. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by