I have a problem with plotting two vectors, I want to vary 'z' by a certain amount and 'x' by another amount and plot versus each other, how can i make the matrix dimensions match?
2 views (last 30 days)
Show older comments
x = 2:1:25;
y = x^2;
z = 1:1:28;
plot(z,x)
Accepted Answer
More Answers (2)
the cyclist
on 24 Nov 2018
Well, it's really up to you to decide. You have a 24-element vector, and a 28-element vector.
Which of those elements align with each other? The first 24 elements of both? Then you could just drop the last 4 elements of z.
Or maybe the two endpoints align, and you need to linearly interpret both vectors onto the same number of points?
It's really not clear from your question. I think you need to think more carefully about what makes sense.
2 Comments
the cyclist
on 24 Nov 2018
Edited: the cyclist
on 24 Nov 2018
Yes, there are an infinite number of ways to make these two vectors the same length. Here are three:
Method 1:
x = [x zeros(1,4)]; % Append 4 zeros to the end of x.
Method 2:
z = z(1:24); % Use only the first 24 elements of z.
Method 3:
x = [x nan(1,4004)]; % Append 4004 NaNs to the end of x.
z = [z nan(1,4000)]; % Append 4000 NaN to the end of z.
(The last one is almost certainly not useful, but who knows?)
My point is that the best method will depend on the relationship between the current elements of x and z. How do they "line up" with each other? That relationship is impossible to understand without more detailed information.
Ikenna Okoye
on 24 Nov 2018
3 Comments
the cyclist
on 27 Nov 2018
So, you are talking about a pretty major change in your code. You want to take something that was written as a function of one variable, and change it to now be a function two (or maybe three?) variables.
madhan ravi's idea that you will need to use meshgrid is likely part of the solution. Given input of two vectors, that function will create a 2-d grid of all possible combinations of the elements of them.
Then you would use the output of that to build the functions of those two variables. You will probably need to rewrite a lot of lines of code.
See Also
Categories
Find more on Applications in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!