在matlab中绘制一些数据,如成对 [英] plot some data such as pairs in matlab

查看:116
本文介绍了在matlab中绘制一些数据,如成对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一些数据,但不能.

I want to plot some data, but I can't.

假设我们在2列中有820行,分别代表x和y坐标.

It is assumed that we have 820 rows in 2 columns, representing the x and y coordinates.

我的代码如下:

load('MyMatFileName.mat');
[m , n]=size(inputs);
s = zeros(m,2);
for m=1:820        
    if inputs(m,end-1)<2 &   inputs(m,end)<2
        x = inputs(m,end-1)
        y = inputs(m,end)
        plot(x,y,'r','LineWidth',1.5)
        hold on
    end
end

推荐答案

我已经编辑了您的代码并添加了注释,以说明您可以进行的更改,但是请参见下文,编写代码,使其更像应该如何完成:

I've edited your code and added comments to explain the changes you could make, but see below I've also re-written your code to be more like how it should be done:

load('MyMatFileName.mat');  % It's assumed "inputs" is in here
% You don't use the second size output, so use a tilde ~
[m, ~] = size(inputs);
%s = zeros(m,2); % You never use s...

% Use a different variable for the loop, as m is already the size variable
% I've assumed you wanted ii=1:m rather than m=1:820
figure
hold on   % Use hold on and hold off around all of your plotting
for ii=1:m
    if inputs(m,end-1)<2 && inputs(m,end)<2 % Use double ampersand for individual comparison
        x = inputs(m,end-1)
        y = inputs(m,end)
        % Include the dot . to specify you want a point, not a line!
        plot(x, y, 'r.','LineWidth',1.5)
    end
end
hold off

在Matlab中完成整个操作的更好方法是将代码矢量化:

A better way of doing this whole operation in Matlab would be to vectorise your code:

load('MyMatFileName.mat');
[m, ~] = size(inputs);
x = inputs(inputs(:,end-1) < 2 & inputs(:,end) < 2, end-1);
y = inputs(inputs(:,end-1) < 2 & inputs(:,end) < 2, end);
plot(x, y, 'r.', 'linewidth', 1.5);

请注意,这将绘制点,如果要绘制线,请使用

Note that this will plot points, if you want to plot the line, use

plot(x, y, 'r', 'linewidth', 1.5); % or plot(x, y, 'r-', 'linewidth', 1.5);

这篇关于在matlab中绘制一些数据,如成对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆