How to sum unequal matrices of sizes 10X37 and 10X33?

3 views (last 30 days)
Hi, I have unequal matrices, with sizes 10X37 and 10X33, I usually manually fill in the difference between the 37 and 33 with zeros to make it work. However, I have thousands of similar files, which makes the process quite lengthy.
Can anyone please help me in this? I have attached the files to be summed, and provided my code below.
Thank you very much.
P1=importdata('Pier1.txt');
P2=importdata('Pier2.txt');
P3=importdata('Pier3.txt');
Force = [P1]+[P2]+[P3];

Accepted Answer

KSSV
KSSV on 14 Feb 2018
YOu have an option of changing all the matrices to same size by interpolation. Have a look on imresize, with this you can get all matrices to your desired size.
P1 = rand(10,37) ;
P2 = rand(10,33) ;
P1 = imresize(P1,[10 33]) ;
  2 Comments
Ismail Qeshta
Ismail Qeshta on 14 Feb 2018
Thank you KSSV. Unfortunately, this option can't work for my type of analysis. I only need zeroes in the remaining columns to keep the consistency.
Ismail Qeshta
Ismail Qeshta on 14 Feb 2018
Edited: Ismail Qeshta on 14 Feb 2018
Thanks again KSSV. I have got this code from other discussion on similar topic. The code was provided by @Jan Simon. I am just sharing it here for your kind reference and information: The full discussion can be viewed through this link: https://au.mathworks.com/matlabcentral/answers/31292-adding-uneven-matrices#answer_39796
a = ones(150,91)*2
b = ones(141,100)*3
c = AddFill(a, b);
function c = AddFill(a, b);
sa = size(a);
sb = size(b);
c = zeros(max(sa, sb)); % Pre-allocate
c(1:sa(1), 1:sa(2)) = a; % Assign a
c(1:sb(1), 1:sb(2)) = c(1:sb(1), 1:sb(2)) + b; % Add b

Sign in to comment.

More Answers (0)

Categories

Find more on Quadratic Programming and Cone Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!