How can I subtract the values of each two columns in a big matrix?
7 次查看(过去 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!