How to use for loop rather than nesting if loop

I want to use this code to subtract one value from each number in the histogram when all have been selected to find the new number with zero counts. This code worked (since there are two if loops with one nested) for the first two times 1 is subtracted from cdiff, but then returns the error 'Unbalanced or unexpected parenthesis or bracket.' : %check if Cdiff is empty:
if isempty(C_diff)
fprintf('C_diff is empty (all numbers have drawn once) \n');
cdiff_counts= counts - 1;%cdiff_data_vector = reshape(data,[9,numel(data)]);
C_diff = find(cdiff_counts==0);%return vector of values with 0
fprintf('By subtracting 1 count for all numbers, the new C_diff is %d\n', C_diff);
if isempty(C_diff)
fprintf('C_diff is empty (all numbers have drawn once) \n');
cdiff_counts= cdiff_counts - 1;%cdiff_data_vector = reshape(data,[9,numel(data)]);
C_diff = find(cdiff_counts==0);%return vector of values with 0
fprintf('By subtracting 1 count for all numbers, the new C_diff is %d\n', C_diff);
end
else
fprintf('C_diff is %d\n', C_diff);%
end
Replacing the first 'if' with 'for' returns 'Unbalanced or unexpected parenthesis or bracket.'
How can I get this to occur until C_diff is no longer an empty array?
if isempty(C_diff)
fprintf('C_diff is empty (all numbers have drawn once) \n');
cdiff_counts= counts - 1;%cdiff_data_vector = reshape(data,[9,numel(data)]);
C_diff = find(cdiff_counts==0);%return vector of values with 0
fprintf('By subtracting 1 count for all numbers, the new C_diff is %d\n', C_diff);
else
fprintf('C_diff is %d\n', C_diff);%
end
Best regards,
Alex

댓글 수: 2

Jan
Jan 2018년 1월 27일
There are no "if loops" in any programming language.
Alex
Alex 2018년 1월 27일
ok, "if statement"?

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

 채택된 답변

Jan
Jan 2018년 1월 27일
편집: Jan 2018년 1월 27일

0 개 추천

while isempty(C_diff)
cdiff_counts = counts - 1;
C_diff = find(cdiff_counts == 0);
end
It looks like C_diff contains positive values initially, maybe only integers. Then this is more efficient:
minValue = min(counts); % [EDITED]
C_diff = find(C_diff = minValue);

댓글 수: 3

This answer helped, but did not work because in the first if statement cdiff_counts = counts -1, then in subsequently nested statements cdiff_counts = cdiff_counts -1. Here is what worked:
%C_diff
il=1;
while isempty(C_diff)
fprintf('C_diff is empty (all numbers have drawn once) \n');
cdiff_counts = counts - il;
C_diff = find(cdiff_counts == 0);
il=il+1;
end
fprintf('C_diff is %d\n', C_diff);%
Thank you nevertheless!
Jan
Jan 2018년 1월 27일
I had a typo in the min() call. I assume, it is much cheaper. If you provide some meaningful inputs data and the wanted result, I could even test my suggestions.
Alex
Alex 2018년 1월 28일
Thank you! I will keep that in mind for future questions. It is not necessary, but if you are interested, here is the "counts" data for numbers 1:1:49. The number 32 should be returned.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2018년 1월 27일

댓글:

2018년 1월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by