I have to address several issues in your code first:
If you use a cell array, you must use curly braces ({}
) to extract elements from it. Instead of writing colour(i)
you should be writing colour{i}
.
This is not a problem, but it's a matter of good practice. If you don't need to handle the else
part of the if
statement, don't write it at all.
Preallocate memory so that arrays don't grow inside the loop (it slows down the program). Specifically, add the line result = cell(size(colour));
before the for
loop.
Your isequal
logic is flawed. Practically, it would always return false
because colour{1}
is one element and type{:}
is many.
According to your example, types
contain numbers and colours
letters, although they are both strings. Does it make sense to compare the two?
Now, regarding your question, it's up to you to decide how the for
loop runs. Since you don't mention what you want to achieve (you rather ask how you want to achieve something without saying what exactly), I cannot say what your for
loop should look like, if necessary at all. Maybe you meant to use ismember
instead of isequal
? If so, the fixed code can look like this:
result = cell(size(colour));
for i = 1:length(colour)
if ismember(colour{i}, type)
result{i} = type{i};
end
end
or shorter, like this:
result = cell(size(colour));
[found, idx] = ismember(colour, type);
result(found) = type{idx(found)}
If you provide more details, maybe I can refine my answer so that it helps you more.