필터 지우기
필터 지우기

avoid for loop in a specific code

조회 수: 1 (최근 30일)
MA
MA 2019년 8월 10일
댓글: MA 2019년 8월 10일
Could anyone help me if there is any way to avoid for loop in the following code: Both Target and predicted_target are vectors with n rows and one column.
new_classes=cell(size(Target,1),1);
for i=1:size(Target,1)
if (strcmp(predicted_target(i),Target{i}))
new_classes{i}=strcat('c',Target{i});
else
new_classes{i}=strcat('E',Target{i});
end
end
  댓글 수: 3
MA
MA 2019년 8월 10일
imagine that my data is as simple as that:
predicted_t=[ '1'; '0'; '0'; '1'];
Target=[ '0'; '1'; '1'; '1'];
So, I am expecting the new classes to be:
{'E0'}
{'E1'}
{'E1'}
{'c1'}
Bruno Luong
Bruno Luong 2019년 8월 10일
편집: Bruno Luong 2019년 8월 10일
@MA: You change cell array to char array, you change variable name predicted_target to predicted_t.
Please don't do that if you want getting exactly the code that works

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

채택된 답변

Bruno Luong
Bruno Luong 2019년 8월 10일
편집: Bruno Luong 2019년 8월 10일
predicted_target = {'1'; '0'; '0'; '1'};
Target = {'0'; '1'; '1'; '1'};
prefix = {'E'; 'c'}
new_classes = strcat(prefix(strcmp(predicted_target,Target)+1),Target)
  댓글 수: 1
MA
MA 2019년 8월 10일
Thank you. I just change the prefix, so the altered code is the following:
prefix=int2str(predicted_target==Target);
prefix(prefix=='1')='c';
prefix(prefix=='0')='E';
new_classes = strcat(prefix,Target);
new_classes=cellstr(new_classes);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by