code for conditional statement

조회 수: 1(최근 30일)
Esegboria Osarhemen
Esegboria Osarhemen 2019년 3월 9일
댓글: Esegboria Osarhemen 2019년 3월 9일
If I have
a = [1:5], b = [1:5], c =[1:5], d =[1:5], e = zeros(1,4);
How can I write a code for the following condition?
I want to pick the first entries from a,b,c,d and put in 'e', that will be 1 1 1 1. I want the conition were no two entries are the same. If two entries a same, I want to replace it with another value from the array. So I want 'e' to be 1 2 3 4

채택된 답변

Rik
Rik 2019년 3월 9일
You can use code similar to that below. It is easier to put the input data in a cell arrray than to have different names for them. The code below does not check if it is possible to form a unique combination, nor is it guaranteed to find a combination if it is possible.
input_data=repmat({1:5},1,4);
output=zeros(size(input_data));
output(1)=input_data{1}(1);
for n=2:numel(input_data)
k=1;
while any(ismember(output(1:(n-1)),input_data{n}(k)))
k=k+1;
end
output(n)=input_data{n}(k);
end
disp(output)
  댓글 수: 1
Esegboria Osarhemen
Esegboria Osarhemen 2019년 3월 9일
Thanks, the actual data i want to apply the code to is in a cell

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

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by