Combination of sub-matrices generated within a for loop

2 Ansichten (letzte 30 Tage)
Rengin
Rengin am 13 Aug. 2019
Kommentiert: Bruno Luong am 13 Aug. 2019
clear;
clc;
close all;
n=[3 1 2];
rad=[0.04 0.02 0.01];
h=[10 12 15];
for kk=1:length(n)
matrix=zeros(n(kk),n(kk));
for ii=1:n(kk)
for jj=1:n(kk)
val=rad(kk)*h(kk);
matrix(ii,jj)=val;
end
end
end
% Dear users, if you put a debug at the very last "end" (row 17), you can
% see that I aim to generate 3 sub-matrices having the size of 3x3, 1x1 and
% 2x2 (see n=[3 1 2]). I want to combine each sub-matrix one each other diagonally.
% I want to get the matrix as a result below:
matrix=[0.4 0.4 0.4 0 0 0;0.4 0.4 0.4 0 0 0;0.4 0.4 0.4 0 0 0;0 0 0 0.24 0 0;0 0 0 0 0.15 0.15;0 0 0 0 0.15 0.15];
%Is there any simple way to do it?
%Thanks in advance!
  3 Kommentare
madhan ravi
madhan ravi am 13 Aug. 2019
Using the previous solution of your question gives the same result, please describe the situation where that condition contradicts your needs.
Bruno Luong
Bruno Luong am 13 Aug. 2019
"The calculation of Val is different."
You might try to learn deeper how computer programming actually works, if you get stuck everytime the values change then you miss the whole purpose of programming.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

infinity
infinity am 13 Aug. 2019
Hello,
There are various solutions for this question, one of the closet one to your current code could be
clear;
clc;
close all;
n=[3 1 2];
rad=[0.04 0.02 0.01];
h=[10 12 15];
matrix = zeros(sum(n));
for kk=1:length(n)
% matrix=zeros(n(kk),n(kk));
for ii=1:n(kk)
for jj=1:n(kk)
val=rad(kk)*h(kk);
matrix(ii+sum(n(1:kk-1)),jj+sum(n(1:kk-1)))=val;
end
end
end

Weitere Antworten (0)

Kategorien

Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by