How to plot faster instead of for loop?

3 vues (au cours des 30 derniers jours)
galaxy
galaxy le 15 Déc 2022
Commenté : galaxy le 18 Déc 2022
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

Réponse acceptée

Fabio Freschi
Fabio Freschi le 15 Déc 2022
Modifié(e) : Fabio Freschi le 15 Déc 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))
  1 commentaire
galaxy
galaxy le 18 Déc 2022
I think it is OK.
thank you for your anwser.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by