code gives me a false values?
Mostrar comentarios más antiguos
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
Más respuestas (3)
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
Jos
el 17 de Jul. de 2014
0 votos
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
el 17 de Jul. de 2014
0 votos
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!