MATLAB Answers

Changing composition of Row Matrix (1x9) into a 3x3 Matrix

21 views (last 30 days)
Anilcan Taner
Anilcan Taner on 27 Oct 2019
Commented: Daniel M on 27 Oct 2019
Im a newbie matlab student who has a project for solving a hydraulic problem with finite element method in Matlab.
So far seems good, but I am having difficult time since I have no background about algorithm and programming.
My problem here is;
1-) Making K{i} into a K1 K2 K3 [ seems not necessary if im not wrong but it would make me feel much safer :) ],
2-) K{1} to K{9} looks like something like this,
[1 2 3 4 5 6 7 8 9]
and I need them to look like
[1 2 3; 4 5 6; 7 8 9]
Yes, I am an idiot.
Sorry for Turkish explanations.
echo off all
clc
nodeX = [0,5,9.17,12,0,5,9.17,12,0,5,8,9.17,12]; % noktalarin x koordinatlari
nodeY = [8,8,8,8,4,4,5.5,5.5,0,0,0,2.83,4]; % noktalarin y koordinatlari
elemX = [5,6,6,7,7,7,5,9,10,11,6,7,7]; % i icin olusan ucgenlerin global nokta karsiliklari
elemY = [1,1,2,2,3,4,6,6,6,6,7,12,8]; % j icin
elemZ = [6,2,7,3,4,8,9,10,11,12,12,13,13]; % k icin
varTable = [];
fprintf("e xi\txj\txk\tyi\tyj\tyk\tck\tci\tcj\tbi\tbj\tbk\tA\n"); % tablo basligini formatla
for i = 1:length(elemX)
% sirasiyla elemanlari okutuyoruz
currElemX = elemX(i);
currElemY = elemY(i);
currElemZ = elemZ(i);
% i ye karsilik gelen global noktalarin koordinat karsiliklari
% bulunuyor
xi = nodeX(elemX(i));
xj = nodeX(elemY(i));
xk = nodeX(elemZ(i));
yi = nodeY(elemX(i));
yj = nodeY(elemY(i));
yk = nodeY(elemZ(i));
% gerekli hesaplama yapiliyor
ck = xj - xi;
ci = xk - xj;
cj = xi - xk;
bi = yj - yk;
bj = yk - yi;
bk = yi - yj;
% alan hesabi
A = abs(0.5 * (xi * yj + xj * yk + xk * yi - xi * yk - xj * yi - xk * yj));
%dongunun disinda kullanmak icin bulunan degerleri sakla
varTable = [varTable ; ck,ci,cj,bi,bj,bk,A];
% ekrana uygun formatla yazdiriliyor
fprintf("%d %.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.4f\t\n",i,xi,xj,xk,yi,yj,yk,ck,ci,cj,bi,bj,bk,A);
end
Kcal= array2table(varTable, 'Variablenames',{'ck','ci','cj','bi','bj','bk','A'}) %K 3x3 matris hesabi icin tablo
%tablodaki degerleri disari cekiyorum sutunlari ayri olarak
%A='Tablename'.'variablename' komutu ile
%n sembolu sondaki sadece karisiklik onlemek amacli
cint=Kcal.ci;
cjnt=Kcal.cj;
cknt=Kcal.ck;
bint=Kcal.bi;
bjnt=Kcal.bj;
bknt=Kcal.bk;
Ant=Kcal.A;
%Transpoze ediyoruz satira cevirmek icin sutun matrisini
bin=bint.';
bjn=bjnt.';
bkn=bknt.';
cin=cint.';
cjn=cjnt.';
ckn=cknt.';
An=Ant.';
%Yazdiracagimiz icin basliklar \t ile tab bosluk biraktirliyor \n ile yeni
%satir yazdiriliyor.
fprintf("e K11\tK12\tK13\tK21\tK22\tK23\tK31\tK32\tK33\n"); % tablo basligini formatla
%Degiskenler icin bos matris
varTable2 = [];
for i=1:height(Kcal) % Kcal sutun olarak dizildigi icin height kullandik.
currcin=cin(i);
currcjn=cjn(i);
currckn=ckn(i);
currbin=bin(i);
currbjn=bjn(i);
currbkn=bkn(i);
currAn=An(i);
cinn=cin(i);
cjnn=cjn(i);
cknn=ckn(i);
binn=bin(i);
bjnn=bjn(i);
bknn=bkn(i);
Ann=An(i);
% K Row x Column olarak isimlendirir isek, 3x3 matrisin degerleri
K11=(1/(4 * Ann)) * ( binn^2 + cinn^2);
K12=(1/(4 * Ann)) * ((binn * bjnn) + (cinn * cjnn));
K13=(1/(4 * Ann)) * ((binn * bknn) + (cinn * cknn));
K21=(1/(4 * Ann)) * ((binn * bjnn) + (cinn * cjnn));
K22=(1/(4 * Ann)) * ((bjnn)^2 + (cjnn)^2);
K23=(1/(4 * Ann)) * ((bjnn * bknn) + (cjnn * cknn));
K31=(1/(4 * Ann)) * ((binn * bknn) + (cinn * cknn));
K32=(1/(4 * Ann)) * ((bjnn * bknn) + (cjnn * cknn));
K33=(1/(4 * Ann)) * ((bknn)^2 + (cknn)^2);
varTable2 = [varTable2 ; K11, K12,K13, K21, K22, K23, K31, K32, K33]; %Degiskenleri tabloya aliyoruz
%%%%%%%%%%%%%%%%% YAZDIRMADA SIKINTI VAR %%%%%%%%%%%%%%%
fprintf("%d %.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t%.4f\t\n",i,K11,K12,K13,K21,K22,K23,K31,K32,K33);
%%%%%%%%%%%%%%%%% YAZDIRMADA SIKINTI VAR %%%%%%%%%%%%%%%
end
Kcal2= array2table(varTable2, 'Variablenames',{'K11','K12','K13','K21','K22','K23','K31', 'K32', 'K33'}) %K 3x3 matris hesabi icin tablo2
%K matrislerini olusturmak icin verilerimiz hazir.
%Siradaki islem bu verileri 3x3 Matrislere yerlestirmek.
% 1.SIRA: K1 K2 K3 VERILERINI AYRI AYRI CEKMEK
%%%%%%%%%%%%%%%%%% BURADA KOPUYORUM %%%%%%%%%%%%%%%%%%%%%%
varTable3 = [];
for i=1:height(Kcal2)
varK = [1:1:i]; %1 den i ye kadar rakamlardan olusan satir matrisi yazdirdik
K{i}=varTable2(i,:); %3x3 matrisleri K{1} K{2} olarak ayirir row matrisi olarak
end
K1=K{1}

Accepted Answer

Anilcan Taner
Anilcan Taner on 27 Oct 2019
Problem is solve with the help of reshape function,
However, problem never ends in a newbie world.
with the code of;
K{i}=varTable2(i,:);
I created K1 K2 K3 in cell form but i want them in a double form indeed.
So that's my second question.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by