Can someone help me out with writing a for loop? I'll do my best to explain. So, I need to take the first row of a 20x4096 single and compare it with row 2, then row 3, and then row 4 using cosine similarity where row 1 = x and rows 2 through 4 = y. Then I need to take row 5, compare it with row 6, then row 7, then row 8 and continue that process until I get through all 20 rows. Hopefully that all makes sense. I can provide whatever is needed.

답변 (1개)

James Tursa
James Tursa 2020년 12월 12일
편집: James Tursa 2020년 12월 12일

0 개 추천

You could take this approach:
A = your matrix
AA = A*A'; % all of the dot products between rows (more than you need, actually)
d = sqrt(diag(AA)); % the norms of the rows
AA = AA ./ d; AA = AA ./ d'; % normalize everything, or AA = AA ./ (d*d');
The result has the cosine similarities between all of the rows. Pick off what you need. E.g., AA(1,2) has the cosine similarity between rows 1 and 2. AA(5,8) has the cosine similarity between rows 5 and 8. Etc.
This does extra computations that you don't need, of course. But by using functions like matrix multiply that are multi-threaded I am guessing it may run faster than custom code that tries to avoid the unnecessary computations. The caveat is if the row size is too large then the memory usage may bog you down.

댓글 수: 9

Christian Basa
Christian Basa 2020년 12월 12일
How would I be able to implement this into a for loop? The data I'm working with is just example data, I want to make sure it works before I use it on the actual data which is a lot larger.
James Tursa
James Tursa 2020년 12월 12일
편집: James Tursa 2020년 12월 12일
Can't you just run my code and do some spot calculations to satisfy yourself that it works? Why do you need to use for-loops? E.g., compare the following:
AA(1,2) - dot(A(1,:),A(2,:))/(norm(A(1,:))*norm(A(2,:)))
AA(1,3) - dot(A(1,:),A(3,:))/(norm(A(1,:))*norm(A(3,:)))
AA(5,7) - dot(A(5,:),A(7,:))/(norm(A(5,:))*norm(A(7,:)))
etc.
If you really want a for-loop
for m = 1:4:size(A,1)
for k = m+1:m+3
result(m,k) = dot(A(m,:),A(k,:)) / (norm(A(m,:))*norm(A(k,:)));
end
end
The result should be pretty close to the AA values in the spots that are calculated.
On my machine, the matrix multiply code I posted above runs about 10 times faster than the loops shown for a large matrix, even though the matrix multiply code is calculating many more spots than the looping code.
Christian Basa
Christian Basa 2020년 12월 12일
편집: Christian Basa 2020년 12월 12일
I tried it and it doesn't like any values greater than 5. So AA(5,5) works but AA(5,6) doesn't. It makes sense for the first number to not be able to go up to 5, but I have 15 rows that need to be compared. What I'm trying to do is take the first row as X and the second, third, fourth row as Y and then run it through my cosine similarity function and repeat that until it's through all the data. This is what I have written for cosine similarity.
num = sum(X.*Y);
den = sqrt(sum(X.^2).*sqrt(sum(Y.^2));
csim = num/den;
An then I need to store those results so I can plot a histogram. That's kinda why I wanted to do a for loop.
AA is creating a 5x5 single.
James Tursa
James Tursa 2020년 12월 13일
What is size(A)?
Christian Basa
Christian Basa 2020년 12월 13일
A is a 5x4096 single. I did A = featuresTest(1:4:end,:); Cause I need rows 1 5 9 13 17 so I can compare row 1 with 2,3,4. Row 5 with 6,7,8 and so on.
I have something like this:
i = featuresTest(1:4:end,:);
k = featuresTest([2:4,6:8,10:12,14:16,18:20],:);
x = 1:5;
for j = 1:5
X(j,:) = i(j,:);
for l = k(1:3:end)
num = sum(X(j,:).*l(k,:));
den = sqrt(sum(X(j,:).^2))*sqrt(sum(l(k,:).^2));
csim = num/den;
end
end
But i keep getting confused with variables, so I'm trying to figure that out.
James Tursa
James Tursa 2020년 12월 13일
Huh? I'm lost. If A is 5x4096 it only has 5 rows. Why are you talking about rows 9, 13, 17?
Christian Basa
Christian Basa 2020년 12월 13일
The entire dataset featuresTest is 20x4096. I'm storing rows 1 5 9 13 and 17 into A and trying to access that. It does only have 5 rows, but it contains [1,5,9,13,17] inside of it.
James Tursa
James Tursa 2020년 12월 13일
편집: James Tursa 2020년 12월 13일
Well, forget variable A. Simply use my code with featuresTest.
I would point out that this is the first time you have bothered to mention to me that you have extracted every 4th row into a separate variable and are trying to work with that. This would have been an important detail to mention up front.
Christian Basa
Christian Basa 2020년 12월 13일
편집: Christian Basa 2020년 12월 13일
Is there a better way to store all those results into an array so I can plot the histogram for it? Or should I just do A(1,2:4), save that as a variable and then put it into an array?
Yeah, sorry. I didn't realize I forgot to mention it, so I understand why it could have been confusing. I didn't have much written up for the code yet, but what I posted is what I was working on.

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

카테고리

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

질문:

2020년 12월 12일

편집:

2020년 12월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by