If-statement = true then replace column with specfic value - How to?

Hey
I'm trying to replace a whole column with a specific value if the number already appears in the column
Any suggestions?
My current output is the same as the input except for the one space where -3 appears it replaces it with 0 for some reason
m = size(input, 1);
n = size(input, 2);
for i = 1:n
for j = 1:m
if (input(i,j) == -3)
inputTemp(i,:) = -3;
i = i + 1;
else
inputTemp(i,j)= input(i,j);
end
end
end

댓글 수: 2

Hello,
Why do you put
i = i + 1;
in your code?
Because if -3 appears once in the column it needs to replace the whole column with -3 and then go to the next :)

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

 채택된 답변

KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 6월 25일
편집: KALYAN ACHARJYA 2019년 6월 25일
[m n]=size(input);
for i=1:m
for j=1:n
if input(i,j)==-3
inputTemp(i,:)=-3;
end
end
end
Same code can be implemeneted without loop-Recomended
inputTemp?? Please note you are not going to "replace column with specfic value"
As per the code, when any element =-3, then only respective row elements (definitely all columns) are forced to -3.

댓글 수: 1

Ah yeah, I see - nice! I've got it working. Thanks for your time :-)
[m n]=size(input);
for i=1:m
for j=1:n
if input(i,j)==-3
input(:,j)=-3;
end
end
end

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by