How do I make an if statement based on whether an index is part of a vector of numbers?

20 次查看(过去 30 天)
Hi, I am using MATLAB R2020a on a MacOS. I am trying to write an 'if' statement such that a command is executed if the index of the for loop if not a part of a vector of values. I have a vector 'abnormal_attractors' which has the index positions of indices that I do not want the command to be executed. I only what the command to be executed for all the index positions apart from these values.
I would very much appreciate any suggestions. Thanks in advance
for currentcycle = 2:length(number_cycles)
abnormal_attractors = [95, 94, 93, 92, 91, 90, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 67, 66, 65, 64, 25, 24, 23, ...
5, 4, 2];
% if currentcycle is not a member of abnormal_attractors vector, then
% execute following 2 lines
previous_expmean_v = current_expmean_v;
previous_expmean_w = current_expmean_w;
end

采纳的回答

James Tursa
James Tursa 2020-12-2
编辑:James Tursa 2020-12-2
You can use the ismember( ) function for this. E.g.,
if ~ismember(currentcycle,abnormal_attractors)
previous_expmean_v = current_expmean_v;
previous_expmean_w = current_expmean_w;
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by