How to solve this only using looping
조회 수: 1 (최근 30일)
이전 댓글 표시
For example A=(aa bb cc dd) and B=(aa bb xx yy zz) then i want to display like this (xx yy zz)
How to display like that by using looping only...??
댓글 수: 0
채택된 답변
Image Analyst
2018년 9월 24일
Try this:
A = [1,2,3,4]
B = [1,2,8,9,10]
lengthA = length(A);
lengthB = length(B);
maxLength = max([lengthA, lengthB])
for k = 1 : maxLength
if k <= lengthA && k <= lengthB
if A(k) ~= B(k)
fprintf('%.1f ', B(k));
end
elseif k > lengthA && k <= lengthB
fprintf('%.1f ', B(k));
elseif k <= lengthA && k > lengthB
fprintf('%.1f ', A(k));
end
end
fprintf('\n');
댓글 수: 2
Image Analyst
2018년 9월 24일
You'd need to convert the string to numbers with something like sscanf() or textscan(), or strsplit() along with str2double().
추가 답변 (1개)
Bish Erbas
2018년 9월 24일
편집: Bish Erbas
2018년 9월 24일
One way of accomplishing this would be:
A = [1 2 3 4 8 9];
B = [1 2 3 4 5 6 7];
for k = 1:numel(A)
B(B==A(k))=[];
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!