Error using horzcat Dimensions of arrays being concatenated are not consistent.

Hi,
Please help me to solve this error?
If I write my code for a single array, it works well. The code is
VD = depth; rho = Rho(:,1);
ac = (1./Vp(:,1).*10^6).*0.000305;
VES2 = (1/0.0313)*log(0.38./Phi(:,1)); % Vertical effective stress by using Athy 1930, fo shale only.
vd = [0;VD]; vd1 = vd(1:end-1); vd2 = vd(2:end);
Z = vd2-vd1; % Thickness
OBP = Z.*rho.*0.0981; % OBP via density equation
OBP_i = 0;
OBP_b = cumsum([OBP_i OBP']);
OBP_b = OBP_b';
OBP_b = OBP_b(2:end); % OBP in bars
But if my Rho, Vp, ac, and Phi are matrix of size 165 85, and depth is of size 165 1. The following error appears in the code below
VD = depth; rho = Rho;
ac = (1./Vp.*10^6).*0.000305;
VES2 = (1/0.0313)*log(0.38./Phi); % Vertical effective stress by using Athy 1930, fo shale only.
vd = [0;VD]; vd1 = vd(1:end-1); vd2 = vd(2:end);
Z = vd2-vd1; % Thickness
OBP = Z.*rho.*0.0981; % OBP via density equation
OBP_i = 0;
OBP_b = cumsum([OBP_i OBP']);
OBP_b = OBP_b';
OBP_b = OBP_b(2:end);
The error is
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in MCMC2D_Test (line 53)
OBP_b = cumsum([OBP_i OBP']);

 Accepted Answer

In the code section where you combine OBP_i and OBP
OBP_i=0;
OBP_b = cumsum([OBP_i OBP']);
when you only consider a single column, OBP' is a row vector and so can put a 0 in front of it and all is well.
But, when you operate on the whole as an array, then OBP is a 2D array and OBP' is also a 2D array and so you're trying to add a single 0 to an array. To make that work as written would require
OBP_i=zeros(size(1,OBP,2));
OBP_b = cumsum([OBP_i.' OBP.'].');
Now, whether you'll be summing over the right direction is probably doubtful, it's not clear why you transposed OBP in the 1D case instead of writing
OBP_b = cumsum([OBP_i; OBP]);
I'd guess that what you really want instead of the above would be
OBP_i=zeros(size(1,OBP,2));
OBP_b = cumsum([OBP_i; OBP]);
instead and the result would be the cumulative sums, also by column.
One minor syntax note; instead of the following code snippet
...
vd = [0;VD]; vd1 = vd(1:end-1); vd2 = vd(2:end);
Z = vd2-vd1; % Thickness
you can use the builtin diff function and write
Z=[0;diff(VD)];
instead and eliminate the two temporary copies of VD.

7 Comments

@dpb by doing so erro r is here
Matrix dimensions must agree.
Error in MCMC2D_Test (line 61)
PP = OBP_MPa - VES2; % Pore pressure by the difference of OBP and Vertical stress.
size of VES2 is 165x85 double (this is correct)
size of 1x14189 double
How can I make them same?
Can you help to solve, I have attached my data
Thanks in advance
Error in MCMC2D_Test (line 61)
PP = OBP_MPa - VES2; % Pore pressure by the difference of OBP and Vertical stress.
There's no variable OBP_MPa in the previous code so we can't tell anything about it.
But
>> 165*85 == 14189
ans =
logical
0
>> 165*85
ans =
14025
>>
so the short answer is "you can't" -- they're not commensurate in total number of elements.
But, as @David Hill notes, we also don't have the Clairvoyant Toolbox yet so we don't have any idea what you're intentions are here -- I made the presumption given the original code that "works" for a given column that the intent is to treat each column in the array the same way; his modification turns the 2D array into a vector overall with
OBP_b = cumsum([OBP_i OBP(:)'])';
and sums over the entire array from beginning to end whereas my code will sum up each column as if had operated on the array as your first code would if you were to use it inside an array over the number of columns in rho, vp, etc., that you selected a single column out of as rho(:,1).
for i=1:size(rho,2) % number columns in original rho, etc., arrays
RHO=rho(:,i); % select given array column...
...
if you did similarly with the other variables and then used these instead, your first code would run. Now, whether it's the right overall answer or not, we can't tell; we don't know the problem definition.
@dpb I have input data e.g., depth, Rho, Vp, Phi and this data is in the form matrices i.e. 85 column. By using this data I want to comput VES_mpa (which will have 85 coulmn as well). The code given below is for (works well) for single array (one column)... I just need help how I can modify it for a matric.
In the above input data, depth is 165 1, by adding duplicate columns its size will also be 165 85, mean depth has same 85 colmun...
%% Computing effective pressure
VD = depth; rho = Rho;
ac = (1./Vp.*10^6).*0.000305;
VES2 = (1/0.0313)*log(0.38./Phi); % Vertical effective stress by using Athy 1930, fo shale only.
vd = [0;VD]; vd1 = vd(1:end-1); vd2 = vd(2:end);
% %Z=[0;diff(VD)];
Z = vd2-vd1; % Thickness
OBP = Z.*rho.*0.0981; % OBP via density equation
OBP_i = 0;
OBP = OBP';
% %OBP_i=zeros(size(Z));
OBP_b = cumsum([OBP_i OBP(:)'])';
% OBP_b = OBP_b';
OBP_b = OBP_b(2:end); % OBP in bars
OBP_MPa = OBP_b./10; % OBP in MPa
OBP_psi = OBP_b.*14.503778;
OBPg = OBP_psi./(VD.*3.28083); % AZ Thesis overburden pressure gradient normal (hydrostatic pressure) gradient
PP = OBP_MPa - VES2; % Pore pressure by the difference of OBP and Vertical stress.
Png = 0.42; dt_ml = 200; dt_m = 77;
PP_e = OBPg-(OBPg-Png).*((dt_m+(dt_ml-dt_m).*exp(-0.000245.*VD.*3.28083))./ac).^0.5;
PP_epsi = VD.*3.28.*PP_e;
VES2 = OBP_psi - PP_epsi; VES_mpa = VES2.*0.0068947573; % VES3 is vertical effective stress in MPa
" I have input data e.g., depth, Rho, Vp, Phi and this data is in the form matrices i.e. 85 column. By using this data I want to comput VES_mpa (which will have 85 coulmn as well). The code given below is for (works well) for single array (one column)... I just need help how I can modify it for a matri[x]."
If it works as intended for a given column, then the expedient solution would be to turn that code into a function and pass each column of the arrays into it as vectors in turn, saving the output vector each time in an array.
That's a straightforward process, simply take the above code and add the function statement at the beginning and end at the end and add the argument list, return variable(s)
function VES_mpa=computeVES(depth, Rho, Vp, Phi)
% Compuutes VES_mpa given column vectors depth, Rho, Vp,Phi
...
% your working code f(depth, Rho, Vp, Phi) here
...
VES_mpa=VES_mpa(:); % ensure return column vector
end
You'll use it something like...
VES_mpa=zero(size(Rho)); % preallocate output; I'm guessing this is size expected?
for i=1:size(VES_mpa,2) % iterate over columns
VES_mpa(:,i)=computeVES(depth,Rho(:,i),Vp(:,i),Phi(:,i)); % call your function each colum in turn
end
Done. The above presumes as in your first post, that there's only a vector of depths, not a full array; if it is also an array, then pass it by column, too.
It shouldn't be too hard to vectorize the function to operate over the array by column.
I'm pretty sure the changes I gave you before are correct up to that point; it doesn't look like you've taken some of them to heart as yet, though, you've still left the internal reorientation of the column vector to a row vector which is OK with a vector but as noted simply won't work as intended with an array.
Also the duplication of the depth vectors is still in that last code instead of simplifying things as much as can to reduce clutter and make the code easier to debug/maintain.
BUT you'll need to just slow down and THINK through what you're actually doing CAREFULLY instead of just poking at it and write down the algebra of the calculation. It often can help to sketch out on paper the orientations and sizes of the inputs and results as you go along instead of just trying to keep in head and bang out code at the editor and throw it at the compiler.
One thing that undoubtedly would help in debugging would be to work on a test input dataset of MUCH smaller size (5 or so depths and 2-3 columns) would be the same as 165x85 as far as the code logic yet small enough you can see the whole thing on the screen to do hand calculations and see what is actually happening.
@dpb thanks, I did as you suggested and it is working, see if I done it correctly
function VES_mpa=computeVES(VD, RhoTime, VpTime, PhiTime)
%
rho = RhoTime; Vp = VpTime; Phi = PhiTime;
ac = (1./Vp.*10^6).*0.000305;
VES2 = (1/0.0313)*log(0.38./Phi); % Vertical effective stress by using Athy 1930, fo shale only.
vd = [0;VD]; vd1 = vd(1:end-1); vd2 = vd(2:end);
Z = vd2-vd1; % Thickness
OBP = Z.*rho.*0.0981; % OBP via density equation
OBP_i = 0;
OBP_b = cumsum([OBP_i OBP']);
OBP_b = OBP_b';
OBP_b = OBP_b(2:end); % OBP in bars
OBP_MPa = OBP_b./10; % OBP in MPa
OBP_psi = OBP_b.*14.503778;
OBPg = OBP_psi./(VD.*3.28083); % AZ Thesis overburden pressure gradient normal (hydrostatic pressure) gradient
PP = OBP_MPa - VES2; % Pore pressure by the difference of OBP and Vertical stress.
Png = 0.42; dt_ml = 200; dt_m = 77;
PP_e = OBPg-(OBPg-Png).*((dt_m+(dt_ml-dt_m).*exp(-0.000245.*VD.*3.28083))./ac).^0.5;
PP_epsi = VD.*3.28.*PP_e;
VES2 = OBP_psi - PP_epsi; VES_mpa = VES2.*0.0068947573; % VES3 is vertical effective stress in MPa
VES_mpa = VES_mpa(:);
end
VES_mpa=zeros(size(RhoTime)); % preallocate output; I'm guessing this is size expected?
for i=1:size(VES_mpa,2) % iterate over columns
VES_mpa(:,i)=computeVES(VD, RhoTime(:,i), VpTime(:,i),PhiTime(:,i)); % call your function each colum in turn
end
You can hand verify the results as easily (more easily, actually) than I...

Sign in to comment.

More Answers (1)

VD = depth; rho = Rho;
ac = (1./Vp.*10^6).*0.000305;
VES2 = (1/0.0313)*log(0.38./Phi); % Vertical effective stress by using Athy 1930, fo shale only.
vd = [0;VD]; vd1 = vd(1:end-1); vd2 = vd(2:end);
Z = vd2-vd1; % Thickness
OBP = Z.*rho.*0.0981; % OBP via density equation
OBP_i = 0;
OBP=OBP';
OBP_b = cumsum([OBP_i OBP(:)'])';%OBP is a maxtrix, OBP(:) converts to an array that can be combined OBP_i
OBP_b = OBP_b(2:end);

3 Comments

@David Hill This is a complete code and size of final VES_mpa should be 165 85, but when I run, again error appears as below (i have attached my data as well, I urgently need if you can help me):
Matrix dimensions must agree.
Error in MCMC2D_Test (line 58)
OBPg = OBP_psi./(VD.*3.28083); % AZ Thesis overburden pressure gradient normal (hydrostatic pressure) gradient
The code is
%% Computing effective pressure
VD = Depth.*1000; rho = Rho;
ac = (1./Vp.*10^6).*0.000305;
VES2 = (1/0.0313)*log(0.38./Phi); % Vertical effective stress by using Athy 1930, fo shale only.
vd = [0;VD]; vd1 = vd(1:end-1); vd2 = vd(2:end);
Z = vd2-vd1; % Thickness
OBP = Z.*rho.*0.0981; % OBP via density equation
OBP_i = 0;
OBP_b = cumsum([OBP_i OBP']);
OBP_b = OBP_b';
OBP_b = OBP_b(2:end); % OBP in bars
OBP_MPa = OBP_b./10; % OBP in MPa
OBP_psi = OBP_b.*14.503778;
OBPg = OBP_psi./(VD.*3.28083); % AZ Thesis overburden pressure gradient normal (hydrostatic pressure) gradient
PP = OBP_MPa - VES2; % Pore pressure by the difference of OBP and Vertical stress.
Png = 0.42; dt_ml = 200; dt_m = 77;
PP_e = OBPg-(OBPg-Png).*((dt_m+(dt_ml-dt_m).*exp(-0.000245.*VD.*3.28083))./ac).^0.5;
PP_epsi = VD.*3.28.*PP_e;
VES2 = OBP_psi - PP_epsi; VES_mpa = VES2.*0.0068947573; % VES3 is vertical effective stress in MPa
Below runs. You need to tell us what you are trying to do with OBP_b (we cannot read your mind).
Rho=randi(100,165,85);
Vp=randi(100,165,85);
ac=randi(100,165,85);
Phi=randi(100,165,85);
depth=randi(100,165,1);
VD = depth; rho = Rho;
ac = (1./Vp.*10^6).*0.000305;
VES2 = (1/0.0313)*log(0.38./Phi); % Vertical effective stress by using Athy 1930, fo shale only.
vd = [0;VD]; vd1 = vd(1:end-1); vd2 = vd(2:end);
Z = vd2-vd1; % Thickness
OBP = Z.*rho.*0.0981; % OBP via density equation
OBP_i = 0;
OBP=OBP';
OBP_b = cumsum([OBP_i OBP(:)'])';%OBP is a maxtrix, OBP(:) converts to an array that can be combined OBP_i
OBP_b = OBP_b(2:end);
@David Hill I have input data e.g., depth, Rho, Vp, Phi and this data is in the form matrices i.e. 85 column. By using this data I want to comput VES_mpa (which will have 85 coulmn as well). The code given below is for (works well) for single array (one column)... I just need help how I can modify it for a matric.
In the above input data, depth is 165 1, by adding duplicate columns its size will also be 165 85, mean depth has same 85 colmun...
%% Computing effective pressure
VD = depth; rho = Rho;
ac = (1./Vp.*10^6).*0.000305;
VES2 = (1/0.0313)*log(0.38./Phi); % Vertical effective stress by using Athy 1930, fo shale only.
vd = [0;VD]; vd1 = vd(1:end-1); vd2 = vd(2:end);
% %Z=[0;diff(VD)];
Z = vd2-vd1; % Thickness
OBP = Z.*rho.*0.0981; % OBP via density equation
OBP_i = 0;
OBP = OBP';
% %OBP_i=zeros(size(Z));
OBP_b = cumsum([OBP_i OBP(:)'])';
% OBP_b = OBP_b';
OBP_b = OBP_b(2:end); % OBP in bars
OBP_MPa = OBP_b./10; % OBP in MPa
OBP_psi = OBP_b.*14.503778;
OBPg = OBP_psi./(VD.*3.28083); % AZ Thesis overburden pressure gradient normal (hydrostatic pressure) gradient
PP = OBP_MPa - VES2; % Pore pressure by the difference of OBP and Vertical stress.
Png = 0.42; dt_ml = 200; dt_m = 77;
PP_e = OBPg-(OBPg-Png).*((dt_m+(dt_ml-dt_m).*exp(-0.000245.*VD.*3.28083))./ac).^0.5;
PP_epsi = VD.*3.28.*PP_e;
VES2 = OBP_psi - PP_epsi; VES_mpa = VES2.*0.0068947573; % VES3 is vertical effective stress in MPa

Sign in to comment.

Categories

Asked:

on 29 Sep 2022

Edited:

dpb
on 29 Sep 2022

Community Treasure Hunt

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

Start Hunting!