How can I subtract the values of each two columns in a big matrix?
조회 수: 6 (최근 30일)
이전 댓글 표시
I have a matrix 13*8 with max and min temperatures. I want to subtract the value of first column from the value of second column for each two columnas and for all rows.
for Exp:
in column 1 the first row: 11.15 - ( -0.5)
in column 3 the first row: 7.89 - ( -2.7)
I obtained the mean with this code:
r = rand(16, 10000);
s = reshape(r, 16, 1000, 10);
t = squeeze(mean(s, 2));
but I don't know what should I do for the substraction.
could you please help me?
댓글 수: 2
Adam
2019년 8월 2일
Sounds like just something like
myMatrix( :, 1:2:end ) - myMatrix( :, 2:2:end);
should work, where myMatrix is your matrix loaded into Matlab, and assuming it always had an even number of columns, as in your example.
답변 (1개)
Steven Lord
2019년 8월 2일
I'm going to make a 10-by-4 matrix of sample data consisting of integer values between 0 and 100 inclusive.
A = randi([0 100], 10, 4)
To subtract one column from another:
threeMinusOne = A(:, 3)-A(:, 1)
If you're using release R2016b or later, if you want to subtract one column from all the other columns, you can do that using implicit expansion. For earlier releases you can do the same thing, but it's a bit more verbose and complicated since you'd need to use the bsxfun function.
allMinusOne = A - A(:, 1)
allMinusOnePre16b = bsxfun(@minus, A, A(:, 1))
doTheyMatch = isequal(allMinusOne, allMinusOnePre16b)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!