How to plot faster instead of for loop?

3 views (last 30 days)
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

Accepted Answer

Fabio Freschi
Fabio Freschi on 15 Dec 2022
Edited: Fabio Freschi on 15 Dec 2022
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))

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!