Sum of unequal sized matrices
1 view (last 30 days)
Show older comments
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 Comments
Image Analyst
on 3 Feb 2018
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
on 3 Aug 2018
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.
Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!