How to plot faster instead of for loop?

4 次查看(过去 30 天)
Dear all,
I have data which imported.
I want to plot vector (which does not (0, 0) -> (0,0) or Na) in specified ranges of index.
ex: index = 5768;
data = load('data.mat');
plot([data.data{5768}(1,1) data.data{5768}(1,2)], [data.data{5768}(2,1) data.data{5768}(2,2)], '-ro');
But when I used for loop, it was very slow. Is there any other way to be faster??
data = load('data.mat');
color_list = {'-ro', '-bo', '-ko', '-go','-mo','-co','-yo'};
for cnt= 1:10000
if( (data.data{cnt}(1,1) == 0) && (data.data{cnt}(2,1) == 0) && ...
(data.data{cnt}(1,2) == 0) && (data.data{cnt}(2,2) == 0)) || ...
(isnan(data.data{cnt}(1,1)) || isnan(data.data{cnt}(2,1)) || ...
isnan(data.data{cnt}(1,2)) || isnan(data.data{cnt}(2,2)))
else
plot( [data.data{cnt}(1,1) data.data{cnt}(1,2)], [data.data{cnt}(2,1) data.data{cnt}(2,2)], color_list{mod(cnt,length(color_list)) + 1});
hold on
end
end

采纳的回答

Fabio Freschi
Fabio Freschi 2022-12-15
编辑:Fabio Freschi 2022-12-15
Try using cellfun
clear variables, close all
load data.mat
% index to [0 0; 0 0] matrices
index1 = cellfun(@(x) isequal(x,[0 0; 0 0]), data, 'UniformOutput', true);
% index to matrices with NaN
index2 = cellfun(@(x) any(isnan(x(:))), data, 'UniformOutput', true);
% index to "good" cells
index = ~(index1 | index2);
% plot
figure, hold on
cellfun(@(x)plot([x(1,1) x(1,2)], [x(2,1) x(2,2)],'o-'),data(index))

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by