How to plot faster instead of for loop?

조회 수: 3 (최근 30일)
galaxy
galaxy 2022년 12월 15일
댓글: galaxy 2022년 12월 18일
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))
  댓글 수: 1
galaxy
galaxy 2022년 12월 18일
I think it is OK.
thank you for your anwser.

댓글을 달려면 로그인하십시오.

추가 답변 (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