How can I subtract the values of each two columns in a big matrix?
4 views (last 30 days)
Show older comments
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 Comments
Adam
on 2 Aug 2019
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.
Answers (1)
Steven Lord
on 2 Aug 2019
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)
See Also
Categories
Find more on Develop Apps Using App Designer in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!