Adding smaller matrices into a larger matrix using a loop
Show older comments
I asked this question before too, and I used the solutions given, however I still got several errors so I'm going to give this another shot since my code has too many errors
V9Split = mat2cell(V9,repmat(5,20,1),5)
V9_eff = zeros(20,5);
for i=1:20
V9_eff(i) = V9Split{i}+(((P9-Po).*A9(i))./mdot_core)
end
However the coding did not work. I want the code to produce something like:
V9_eff1 = V9Split{1}+(((P9-Po).*A9(1))./mdot_core)
V9_eff2 = V9Split{2}+(((P9-Po).*A92)./mdot_core);
V9_eff3 = V9Split{3}+(((P9-Po).*A93)./mdot_core);
V9_eff4 = V9Split{4}+(((P9-Po).*A94)./mdot_core);
V9_eff5 = V9Split{5}+(((P9-Po).*A95)./mdot_core);
V9_eff6 = V9Split{6}+(((P9-Po).*A96)./mdot_core);
V9_eff7 = V9Split{7}+(((P9-Po).*A97)./mdot_core);
V9_eff8 = V9Split{8}+(((P9-Po).*A98)./mdot_core);
V9_eff9 = V9Split{9}+(((P9-Po).*A99)./mdot_core);
V9_eff10 = V9Split{10}+(((P9-Po).*A910)./mdot_core);
V9_eff11 = V9Split{11}+(((P9-Po).*A911)./mdot_core);
V9_eff12 = V9Split{12}+(((P9-Po).*A912)./mdot_core);
V9_eff13 = V9Split{13}+(((P9-Po).*A913)./mdot_core);
V9_eff14 = V9Split{14}+(((P9-Po).*A914)./mdot_core);
V9_eff15 = V9Split{15}+(((P9-Po).*A915)./mdot_core);
V9_eff16 = V9Split{16}+(((P9-Po).*A916)./mdot_core);
V9_eff17 = V9Split{17}+(((P9-Po).*A917)./mdot_core);
V9_eff18 = V9Split{18}+(((P9-Po).*A918)./mdot_core);
V9_eff19 = V9Split{19}+(((P9-Po).*A919)./mdot_core);
V9_eff20 = V9Split{20}+(((P9-Po).*A920)./mdot_core);
V9_eff = [V9_eff1; V9_eff2; V9_eff3; V9_eff4; V9_eff5; V9_eff6; V9_eff7; V9_eff8; V9_eff9; V9_eff10; V9_eff11; V9_eff12; V9_eff13; V9_eff14; V9_eff15; V9_eff16; V9_eff17; V9_eff18; V9_eff19; V9_eff20]
The naming isn't a problem because I could just recall using mat2cell I just want the code to add as shown with as few lines as possible Thanks again for help
1 Comment
Stephen23
on 2 Jun 2015
What exactly is V9? Is it a numeric array, or a cell array?
Answers (2)
Try this:
V9Split = mat2cell(V9,repmat(5,20,1),5)
V9_eff = zeros(20,5);
for k = 1:20
V9_eff(k,:) = V9Split{k}+(((P9-Po).*A9(k))./mdot_core)
end
I made two changes:
- replaced V9_eff(k) with V9_eff(k,:), because you cannot allocate a vector of values into one scalar location of V9_eff. The syntax |V9_eff(k) is only one matrix element, yet you are trying to put multiple elements into this location! This is always going to be an error. The syntax V9_eff(k,:) instead fills the entire row with all of the values from the calculation.
- replaced the loop variable i with k, because i and j are the inbuilt imaginary unit and should not be used for variable names.
2 Comments
Sundus Awan
on 2 Jun 2015
Guillaume
on 2 Jun 2015
Is A9 a cell array then, if it contains 20 matrices?
Assuming A9 is a cell array (with the same number of cells as V9split):
V9Split = mat2cell(V9,repmat(5,20,1),5)
V9_eff = cellfun(@(V9, A9) V9+((P9-Po).*A9)./mdot_core, V9Split, A9, 'UniformOutput', false);
V9_eff = vertcat (V9_eff{:})
Categories
Find more on Loops and Conditional Statements 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!