Sum of unequal sized matrices

1 次查看(过去 30 天)
Mukund
Mukund 2018-2-3
评论: Guillaume 2018-8-3
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
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
Guillaume 2018-8-3
mukund comment mistakenly posted as an answer moved here:
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 CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by