# code gives me a false values?

1 view (last 30 days)
inti on 17 Jul 2014
Edited: inti on 17 Jul 2014
Hi to all, i have a table which contain 3 columns. my goal is to divise the values of column1 by 64 but i find a value which > 128, i should substract it from 256 . after i should do the same think for the others columns. The problem here when i execute my program until matlab, it gives me a right values for column 2 and colum 3, but for column 1 it gives me a false values.
for x = finale(:,1)
if x > 128
x = x - 256
end
e1 = x / 64
end
for x = finale(:,2)
if x > 128
x = x - 256
end
e1 = x / 64
end
for x = finale(:,3)
if x > 128
x = x - 256
end
e1 = x / 64
end
255 199 23
7 199 23
7 199 23
7 199 23
9 199 23
7 199 23
7 199 23
7 199 23
7 199 23
7 199 23
7 199 23
7 199 23
7 199 23
3 201 25
13 197 23
13 197 23
13 199 19
255 197 23
11 199 23
This is the problem in colum 1 , it gives me these values :
255
7
7
7
9
7
7
7
7
7
7
7
7
3
13
13
13
255
11
1

Geoff Hayes on 17 Jul 2014
There is no need for a for loop (and I don't think that it is working as you expect anyway). Step through the code to see why it is unnecessary.
% initialize x to the first column
x = finale(:,1);
% determine all indices of elements that are greater than 128
idcs = x>128;
% do the subtraction
x(idcs) = x(idcs) - 256;
% do the division
e1 = x/64;
Try the above and see what happens, repeating for each of the three columns.
Though from your question you state that find a value which > 128, i should subtract it from 256. The code finds the number and subtracts 256 from it. Which should it be?

Matz Johansson Bergström on 17 Jul 2014
You want to store the result in the correct positions in the resulting vector. You keep placing the value in e1. It is better to use indexing when you loop with "for", instead of passing the vector finale(:, 1). For instance
finale2 = []; %we don't overwrite finale, just create a new matrix
for i=1:size(finale,1)
if finale(i,1)>126
finale2(i,1)=256-finale(i,1);
end
end
Please note: we have to do this for all three columns in finale. In this way, we step through finale and use "i" to store the element in finale2. Now finale2 contains the correct elements.
Another way to do it (which is more elegant) is to use vectorization.
finale2 = finale;
inds = find(finale2 > 256); %store indices of the elements that are larger than 256
finale2(inds) = 256-finale2(inds);
finale2 = finale2./64; %divide all elements by 64
This code is much more compact and neater and we don't need to check for each column, which is nice.
inti on 17 Jul 2014
Edited: inti on 17 Jul 2014
i make a mistake by writing e1 three times. i'm a searching for e,e1,e2 values
if
for x = finale(:,1)
if x > 128
x = x - 256
end
e = x / 64
end
for x = finale(:,2)
if x > 128
x = x - 256
end
e1 = x / 64
end
for x = finale(:,3)
if x > 128
x = x - 256
end
e2 = x / 64
end
end

Jos on 17 Jul 2014
you can do it for all columns in one go (e1 will be a 3 column matrix)
e1=finale;
e1(e1>128)=e1(e1>128)-256; (or e1(e1>128)=256-e1(e1>128); your description and code are different as Geoff mentioned)
e1=e1./64;
inti on 17 Jul 2014
Edited: inti on 17 Jul 2014
i make a mistake by writing e1 three times. i'm a searching for e,e1,e2 values
if
for x = finale(:,1)
if x > 128
x = x - 256
end
e = x / 64
end
for x = finale(:,2)
if x > 128
x = x - 256
end
e1 = x / 64
end
for x = finale(:,3)
if x > 128
x = x - 256
end
e2 = x / 64
end
end

inti on 17 Jul 2014
Thank you veryyyyyy much.
Now , it works well

### Categories

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

### Tags

No tags entered yet.

### Community Treasure Hunt

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

Start Hunting!