How do I count how many times disp() appears in the command window

6 ビュー (過去 30 日間)
GUOLIANG XIE
GUOLIANG XIE 2020 年 4 月 28 日
回答済み: Aditya Patil 2021 年 2 月 5 日
Hi,
I am doing parallel computing with Matlab, the question is simple, how to count how many times disp() appears in the command window.
For example:
N=100;
count=0;
parfor i=1:N
disp(i);
end
%% Here is the problem, when doing parallel computing, I can't count in the loop. So, I want to to count outside the loop
if disp()
count=count+1;
end

回答 (1 件)

Aditya Patil
Aditya Patil 2021 年 2 月 5 日
parfor understands how to handle addition in parallel. Hence you can simply increment a variable as follows,
N=100;
count = 0;
parfor i = 1:N
count=count+1;
end
If this does not work for any reason, you can create a vector of length N, and use it to count whether i-th iteration called disp.
N = 100;
count = zeros([N, 1]);
parfor i = 1:N
% if disp called
count(i)=1;
end

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by