How can I subtract columns for each row by using a for loop

Hi,
I have a matrix like this:
[1.011 1.004 1.054
1.008 0.998 1.042
0.984 0.988 1.024
1.026 1.006 1.016
1.000 0.996 0.977]
I would like to subtract each column for each row and store these results in a new matrix. How can I do this?
Thanks!

댓글 수: 2

can you expand on what you mean by subtract each column for each row? I do not understand what you're subtracting with.
Ok.
In this example I use 3 columns. I want to subtract the column for each possible combinations for each row. So lets say for row 1, I want subtract col 1 - 2, col 1 - 3, col 2 - 3 and so on. I want for row 2 the same subtractions.

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

 채택된 답변

dpb
dpb 2014년 9월 29일
편집: dpb 2014년 9월 29일
nk=nchoosek(1:size(x,2),2);
dx=zeros(size(x,1),size(nk,1));
for i=1:size(nk,1)
dx=x(:,nk(i,2))-x(:,nk(i,1));
end

추가 답변 (3개)

Joseph Cheng
Joseph Cheng 2014년 9월 29일
편집: Joseph Cheng 2014년 9월 29일
you can use combnk() or nchoosek to determine the combination of column subtraction and perform a for loop for each combination.
X = randi(10,4,3);
combin = combnk(1:size(X,2),2);
for ind = 1:size(X,2)
newX(:,ind) = X(:,combin(ind,1))-X(:,combin(ind,2));
end
Guillaume
Guillaume 2014년 9월 29일
Use nchoosek to get all possible combinations of columns, and use that to calculate your differences:
m = [1.011 1.004 1.054
1.008 0.998 1.042
0.984 0.988 1.024
1.026 1.006 1.016
1.000 0.996 0.977];
colcomb = nchoosek(1:size(m, 2), 2);
coldiff = zeros(size(m, 1), size(colcomb, 1));
for comb = 1:size(colcomb, 1)
coldiff(:, comb) = diff(m(:, colcomb(comb, :)), 1, 2);
end

댓글 수: 7

Thanks! This works great!
Is it possible to give the columns names for each 'spread'? For instance column 1 is named as "1-2"?
Not in a matrix obviously, since it can only contain numbers. You could create a cell array of column names
colnames = cell(size(colcomb, 1));
for comb = 1:size(colcomb, 1)
colnames{comb} = sprintf('%d-%d', colcomb(comb, 2), colcomb(comb, 1));
end
Note that the difference calculated are 2-1, 3-1, 3-2, and I've represented that in the names.
Thanks a lot for the quick responses! It works pretty well!
Jimmy
Jimmy 2014년 9월 29일
편집: Jimmy 2014년 9월 29일
Another interesting issue. Let suppose that I have 100 columns instead of 3. I want to store the best 10 combinations based on the combination value (where small is best). How can I achieve this? I also want to store the best combinations names like the 2-1.
Is this possible with Matlab?
(I accidentally clicked on the answer icon by dpb. I guess all the answers regarding the starter post are the correct answers!)
Stephen23
Stephen23 2014년 9월 29일
편집: Stephen23 2014년 9월 29일
A table lets you put names on the columns.
For what working definition of small? But, basic idea is one of two choices...
a) go ahead and generate all pairs and then compute the comparison statistic and choose the N smallest of those, or,
b) compute each pair and the statistic at same time; after N replace the largest of those kept with the last if new is less; update the maxValue comparison value.
I choose for option a. Since the 10 smallest pairs are different for each row (the rows are basically the time line (t).) In the end I want to trade each pair for 125 days on a rolling window. For that reason I want to know which pairs I am trading.

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

Andrei Bobrov
Andrei Bobrov 2014년 9월 30일
편집: Andrei Bobrov 2014년 9월 30일
X = [1.011 1.004 1.054
1.008 0.998 1.042
0.984 0.988 1.024
1.026 1.006 1.016
1.000 0.996 0.977];
n = nchoosek(1:size(X,2),2);
out = squeeze(diff(reshape(X(:,n'),[],2,3),1,2));

카테고리

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

질문:

2014년 9월 29일

편집:

2014년 9월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by