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 次查看(过去 30 天)
显示 更早的评论
x = 2:1:25;
y = x^2;
z = 1:1:28;
plot(z,x)
采纳的回答
更多回答(2 个)
the cyclist
2018-11-24
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 个评论
the cyclist
2018-11-24
编辑:the cyclist
2018-11-24
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
2018-11-24
3 个评论
the cyclist
2018-11-27
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Applications 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!