code gives me a false values?
조회 수: 3 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Geoff Hayes
2014년 7월 17일
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?
댓글 수: 0
추가 답변 (3개)
Matz Johansson Bergström
2014년 7월 17일
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.
Jos
2014년 7월 17일
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;
참고 항목
카테고리
Help Center 및 File Exchange에서 Dialog Boxes에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!