how to use if comparison

3 views (last 30 days)
Enda Sembiring
Enda Sembiring on 29 Nov 2020
Commented: Enda Sembiring on 30 Nov 2020
my problem is i want to compare like the code below and if the condition is met i want it to keep its current value but if the condition is not met iwant to change the value into 0 but when i run the code all the value turn to zero. Can you correct my code?
for c=2:length(fbtbopfin)
for d=1:length(fbtbopfin)
if fbtbopfin(c,1)==fbtbopfin(d,2)
fbtbopfin(c,1)=fbtbopfin(c,1);
else fbtbopfin(c,1)=0;
end
end
end
here is the fbtbopfin matrix
1 2
2 3
3 4
4 5
5 6
6 7
7 8
0 0
10 11
11 12
12 13
13 14
15 16
16 17
17 18
2 19
19 20
20 21
21 22
3 23
23 24
24 25
6 26
26 27
27 28
29 30
30 31
31 32
9 15
12 22
18 33
25 29

Answers (1)

Walter Roberson
Walter Roberson on 29 Nov 2020
The code is working properly according to the requirements that you stated. You want it to change the value to 0 if the condition is not not met, and it does exactly that.
Suppose that your first column V contains 5 and 3, length 2. You for c=2:2, for d=1:2. V(2) is 3, V(1) is 5, the two are not equal so you set V(2)=0. Then you go on to V(2) vs V(2) and you are comparing the newly written 0 to itself and those are equal so you leave that alone.
If you think about your code you will see that it will zero all entries except the first unless all of the entries are equal to each other.
  2 Comments
Walter Roberson
Walter Roberson on 29 Nov 2020
My guess is that you want logic something like this:
found = false;
for index = lower : upper
if something involving variable(index) is true
found = true;
break;
end
if ~found
set something to 0
end
end
Enda Sembiring
Enda Sembiring on 30 Nov 2020
thanks for the response,sir. I'm still new with matlab is 'found' a function? because i cant find any example in mathwork about that, can you please explain more. Thank you beforehand.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!