Changing data diagonally in matrix based on formula and table

Hello,
Let say I have this matrix:
1 1 1 0 1 0 0 0
1 1 0 1 0 1 0 0
1 0 1 1 0 0 1 0
0 1 1 1 0 0 0 1
1 0 0 0 1 1 1 0
0 1 0 0 1 1 0 1
0 0 1 0 1 0 1 1
0 0 0 1 0 1 1 1
and this table:
kxx kyy kzz poroh
______ ______ ______ _______
1 5 6 80 0.12
2 5 6 80 0.21
3 13 62 23 0.20
4 14 81 14 0.22
5 10 16 63 0.23
6 12 62 36 0.26
7 11 45 41 0.21
8 5 5 5 0.19
and this equation:
(1/visc)*((2*kzz2*kzz1*az2*az1)/(kzz2*az2*delz+kz1*az1*delz)
%% All the variables are constant in this equation except (kzz) < which I have in the table.
this equation represent the diagonal started from column 5, I want to change the ones to the result of the equation based on the table
For example (1) in first row and column 5;
the equation will be
(1/visc)*((2*63*80*az2*az1)/(63*az2*delz+80*az1*delz)
kzz5 = 63 and kzz1 = 80
so based on the location on matrix, the code should take the value from the table.
this is the code that I used to change the data diagonally but it changed it to a constant number
k = -(x*y);
d = diag(XX,k);
n = d;
n(n==1) = 2;
XX = XX - diag(d,k) + diag(n,k);
Sorry if the code is not consistent but this is part of big project

 Accepted Answer

unable to understand what you exactly looking. But this may help you
clc
clear
matrix = [ 1 1 1 0 1 0 0 0
1 1 0 1 0 1 0 0
1 0 1 1 0 0 1 0
0 1 1 1 0 0 0 1
1 0 0 0 1 1 1 0
0 1 0 0 1 1 0 1
0 0 1 0 1 0 1 1
0 0 0 1 0 1 1 1];
Table = [ 1 5 6 80 0.12
2 5 6 80 0.21
3 13 62 23 0.20
4 14 81 14 0.22
5 10 16 63 0.23
6 12 62 36 0.26
7 11 45 41 0.21
8 5 5 5 0.19];
kzz = Table(:,4);
visc = 1;
az1 = 1;
az2 = 1;
delz = 1;
for jj = 1:length(matrix(:,1))
for ii = 1:length(matrix(1,:))
Matrix(jj,ii) = (1/visc)*((2*kzz(ii)*kzz(jj)*az2*az1)/(kzz(ii)*az2*delz+kzz(jj)*az1*delz));
end
end

5 Comments

Note, length(matrix(:, 1)) is simply size(matrix, 1) and length(matrix(1, :)) is simply size(matrix, 2). Why waste time extracting a column/row from the matrix just to get the height/width.
Also
for jj = 1:length(matrix(:,1))
for ii = 1:length(matrix(1,:))
Matrix(jj,ii) = (1/visc)*((2*kzz(ii)*kzz(jj)*az2*az1)/(kzz(ii)*az2*delz+kzz(jj)*az1*delz));
end
end
is simply:
matrix = (1/visc) * (2*kzz.'.*kzz*az2*az1) ./ (kzz.'*az2*delz + kzz*az1*delz);
No need for loops.
Thank you for your time, but this is not what I want, maybe I explained it in a wrong way.
X
1 1 1 0 1 0 0 0
1 1 0 1 0 1 0 0
1 0 1 1 0 0 1 0
0 1 1 1 0 0 0 1
1 0 0 0 1 1 1 0
0 1 0 0 1 1 0 1
0 0 1 0 1 0 1 1
0 0 0 1 0 1 1 1
there are 7 diagonals in this matrix, each diagonal represented by equation, For the diagonal just below the 'X', the equation as follow:
(1/visc)*((2*kzz2*kzz1*az2*az1)/(kzz2*az2*delz+kz1*az1*delz)
the variables are kzz2 and kzz1, the values of those variables in the table,
for the first (1) in the diagonal, in row number 1 and column 5 (just below the 'X'), the code should refer to the table:
in the table, row 1 and column kzz = 80 = kzz1 in the eqn
row 5 and column kzz = 63 = kzz2 in the eqn
for the next (1) << at row 2 and column 6 in the matrix
in the table, row 2 and column kzz = 80 = kzz1 in the eqn
row 6 and column kzz = 36 = kzz2 in the eqn
So each (1) in the diagonal, must be replaced by this equation
hope it's clear, thank you again !
so the matrix should look like this
1 1 1 0 88 0 0 0
1 1 0 1 0 99 0 0
1 0 1 1 0 0 55 0
0 1 1 1 0 0 0 66
1 0 0 0 1 1 1 0
0 1 0 0 1 1 0 1
0 0 1 0 1 0 1 1
0 0 0 1 0 1 1 1
those numbers are assumed, just for explaination
kzz = Table(:,4);
visc = 1; %Replace with original values
az1 = 1; %Replace with original values
az2 = 1; %Replace with original values
delz = 1; %Replace with original values
for jj = 1:4
ii = jj+4;
matrix(jj,ii) = (1/visc)*((2*kzz(ii)*kzz(jj)*az2*az1)/(kzz(ii)*az2*delz+kzz(jj)*az1*delz));
end

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2018b

Asked:

on 23 Mar 2019

Commented:

on 23 Mar 2019

Community Treasure Hunt

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

Start Hunting!