code gives me a false values?

2 visualizaciones (últimos 30 días)
inti
inti el 17 de Jul. de 2014
Editada: inti el 17 de Jul. de 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
spreadsheet of finale
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
Thanks in advance

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 17 de Jul. de 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.
Instead, do the following
% 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?

Más respuestas (3)

Matz Johansson Bergström
Matz Johansson Bergström el 17 de Jul. de 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.
  1 comentario
inti
inti el 17 de Jul. de 2014
Editada: inti el 17 de Jul. de 2014
Thanks for your reply.
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

Iniciar sesión para comentar.


Jos
Jos el 17 de Jul. de 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;
  1 comentario
inti
inti el 17 de Jul. de 2014
Editada: inti el 17 de Jul. de 2014
Thanks for your reply.
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

Iniciar sesión para comentar.


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

Categorías

Más información sobre Dialog Boxes en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by