How to plot faster instead of for loop?

3 visualizzazioni (ultimi 30 giorni)
galaxy
galaxy il 15 Dic 2022
Commentato: galaxy il 18 Dic 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

Risposta accettata

Fabio Freschi
Fabio Freschi il 15 Dic 2022
Modificato: Fabio Freschi il 15 Dic 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))

Più risposte (0)

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by