Sum of unequal sized matrices
1 次查看(过去 30 天)
显示 更早的评论
I have matrix A of fixed size 100x2, and B is a matrix growing in size (row-wise) from 1 to 300 rows. The distance criteria is to be used to find nearest neighbour for every row of A w.r.t. all rows in B. They will be stored as indexes a and b.
The modified A is calculated as A=A+B. Please demonstrate through code, consider A=rand(100,2). B gets formed as max row sum of C=rand(5,2) in every iteration from 1 to 300. I am confused of what to and how to use permute /reshape / repmat
2 个评论
Image Analyst
2018-2-3
Please give, for some low iteration, an example of A, B, and C (use small sizes just to demonstrate). I don't even know how many columns B has or what you consider the "distance" to be. And I don't know if you want summing, or vertical concatenation. An example would surely help. Maybe you want pdist2() to compute distances - I don't know, there are still clouds in my crystal ball.
Guillaume
2018-8-3
It is for an idea, may not be accurate. Main iteration
for itr=1:1000 %Main loop
A=rand(100,2) %Fix sized matrix
for i=1:300
C=rand(5,2) %random matrix
[~,cid]=max(sum(C)) %row wise sum
B(i,:)=C(cid,:)
end
for j=1:100
for k=1:size(B,1)
dist(j,k)=sqrt(A(j,:)-B(k,:).^2) % dist of each row in A to each row in B
end
[st,a]=min(dist) %min distance
st(j)=st
b(j)=B(a,:) % index of nearest row in B
end
A=A+B %this step is problematic for me
end
The main aim is to resize B to match A for addition in case its size is less, equal or more.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!