how to find difference between column elemnts in a matrix ?

Let my matrix is
3 2 14 5
7 1 8 9
11 11 12 13
I need to find the substraction between column 1 and 2 and store the result in coulmn 1. similarily for coulmn 3 and 4 and result will be in coulmn 2. i.e the result should be like this
1 9
6 -1
0 -1
kindly, suggest solution. Thank you.

 채택된 답변

Stephen23
Stephen23 2017년 6월 13일
편집: Stephen23 2017년 6월 14일
A general solution (without hard-coded columns):
>> M(:,1:2:end) - M(:,2:2:end)
ans =
1 9
6 -1
0 -1
EDIT: if you have uint8 image data, then convert to double first:
double(M(:,1:2:end)) - double(M(:,2:2:end))

댓글 수: 6

fine, but it is giving '0' at those places where it should be in negative.
Stephen23
Stephen23 2017년 6월 13일
편집: Stephen23 2017년 6월 13일
@aditya sahu: Please show two values that you think should give a negative output.
Dear stephen, what i mean to say is, in the above example, 8 minus 9 i.e (3rd and 4th column 2nd values) i need -1 as result but it is giving as 0.
@aditya sahu: it is likely that you have an unsigned integer matrix: many images are saved as uint8, and in MATLAB the output of arithmetic using uint8 is also uint8 (for which zero is the lowest value).
If that is the case you could easily convert to signed integer:
int16(M(:,1:2:end)) - int16(M(:,2:2:end))
You should pick an appropriate integer class which can encode all of your data:
double(M(:,1:2:end)) - double(M(:,2:2:end)) would run less risk of overflowing datatypes
Thank you @walter-roberson and @Stephen Cobeldick ,,i got it...my matrix was actually uint8 and i am expacting negative, sorry for my mistake..but thank you for your suggestion...

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2017년 6월 13일
Result = YourMatrix(:,[1 3]) - YourMatrix(:,[2 4])

댓글 수: 1

Thank you, but suppose my matrix is 512*512 size then how to do?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2017년 6월 13일

편집:

2017년 6월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by