What does Assignment has more non-singleton rhs dimensions than non-singleton subscripts mean?
Show older comments
Hey in a project I am trying to build a 3d matrix based off another 3d matrix to find a final temperature profile. The code up to the error is:
X = 0:0.1:5;
Y = 0:0.1:1;
Z = 0:0.1:20;
layer = zeros(length(X),length(Y));
layer1 = layer;
layers = layer;
layer1(:,:) = 52;
layers(:,:) = 22;
temperature_profile(:,:,1) = layer1;
for x = 2:length(Z)
temperature_profile(:,:,x) = layers;
end
layers2 = layer;
layers2(:,:) = 1;
for x = 1:length(Z)
temperature_profile2(:,:,x) = layers2;
end
H = zeros(length(X),length(Y),length(Z));
H(:,:,:) = 0.0000001;
G = ones(length(X),length(Y),length(Z));
V = temperature_profile2-temperature_profile;
t = 0;
Cp = 1;
V = 1;
A = 1;
rho = 1;
h = 1;
T0 = 22;
T1 = 52;
k = 1;
T = V < H;
while T ~= G
for z = 2:length(Z)
for y = 1:length(Y)
for x = 1:length(X)
if z == 1
temperature_profile2(x,y,z) = 52;
elseif z == 2
if y == 1
if x == 1
temperature_profile2(x,y,z) = ((0.00001)./(V.*rho.*Cp)).*(V.*k.*((((temperature_profile(x+2,y,z)-temperature_profile(x,y,z))./(0.2^2)))+(((temperature_profile(x,y+2,z)-temperature_profile(x,y,z))./(0.2^2)))+(((temperature_profile(x,y,z+2)-3.*temperature_profile(x,y,z)+2.*temperature_profile(x,y,z-1))./(4.*(0.2^2))))+A.*h.*(T0-T1)))+temperature_profile(x,y,z);
when it is run the Assignment has more non-singleton rhs dimensions than non-singleton subscripts mean error pops up and i'm not really sure what it means.
Accepted Answer
More Answers (3)
the cyclist
on 8 Oct 2016
0 votes
Your code (after adding 7 "end" statements) has been running for over 10 minutes for me, neither finishing nor crashing. Is it possible to post an example that errors out more quickly?
1 Comment
Walter Roberson
on 8 Oct 2016
The posted code does not change T or G, so if it made it into the while loop at all, it is going to infinite loop because of the while
Image Analyst
on 8 Oct 2016
When I get rid of the while, it runs with no error. Please post all the error - that means ALL the red text, not just part of it like you did.
This runs with no error:
X = 0:0.1:5;
Y = 0:0.1:1;
Z = 0:0.1:20;
layer = zeros(length(X),length(Y));
layer1 = layer;
layers = layer;
layer1(:,:) = 52;
layers(:,:) = 22;
temperature_profile(:,:,1) = layer1;
for x = 2:length(Z)
temperature_profile(:,:,x) = layers;
end
layers2 = layer;
layers2(:,:) = 1;
for x = 1:length(Z)
temperature_profile2(:,:,x) = layers2;
end
H = zeros(length(X),length(Y),length(Z));
H(:,:,:) = 0.0000001;
G = ones(length(X),length(Y),length(Z));
V = temperature_profile2-temperature_profile;
t = 0;
Cp = 1;
V = 1;
A = 1;
rho = 1;
h = 1;
T0 = 22;
T1 = 52;
k = 1;
T = V < H;
% while T ~= G
for z = 2:length(Z)
for y = 1:length(Y)
for x = 1:length(X)
if z == 1
temperature_profile2(x,y,z) = 52;
elseif z == 2
if y == 1
if x == 1
temperature_profile2(x,y,z) = ((0.00001)./(V.*rho.*Cp)).*(V.*k.*((((temperature_profile(x+2,y,z)-temperature_profile(x,y,z))./(0.2^2)))+(((temperature_profile(x,y+2,z)-temperature_profile(x,y,z))./(0.2^2)))+(((temperature_profile(x,y,z+2)-3.*temperature_profile(x,y,z)+2.*temperature_profile(x,y,z-1))./(4.*(0.2^2))))+A.*h.*(T0-T1)))+temperature_profile(x,y,z);
end
end
end
end
end
end
msgbox('Done');
% end
Walter Roberson
on 8 Oct 2016
You define temperature_profile and temperature_profile2 as three dimensional objects.
You define V = temperature_profile2-temperature_profile, so V is a three dimensional object.
Your code has
temperature_profile2(x,y,z) = ((0.00001)./(V.*rho.*Cp)).*(V.*k.*((((temperature_profile(x+2,y,z)-temperature_profile(x,y,z))./(0.2^2)))+(((temperature_profile(x,y+2,z)-temperature_profile(x,y,z))./(0.2^2)))+(((temperature_profile(x,y,z+2)-3.*temperature_profile(x,y,z)+2.*temperature_profile(x,y,z-1))./(4.*(0.2^2))))+A.*h.*(T0-T1)))+temperature_profile(x,y,z);
On the left side we can see assignment to a single location.
On the right side, every reference to temperature_profile and temperature_profile2 are subscripted so those reference are all scalars as well. k and rho and cp and T0 and T1 and A are all scalars as well, so those are fine.
But the code refers to V in a couple of places, and V is a three dimensional object, so the result of the right hand side is going to be a three dimensional object. And you cannot store that three dimensional object into a scalar.
I suspect you want the references to be to V(x,y,z)
It looks to me that you could probably vectorize the code. For example,
temperature_profile(x+2,y,z)-temperature_profile(x,y,z)
could potentially be
temperature_profile(3:end,:,:)-temperature_profile(1:end-2,:,:)
The boundary conditions like filling in the 52 can be handled in vectorized form as well by expanding the matrix by one in that direction and writing in 52 to that slice at each step.
Categories
Find more on Surfaces, Volumes, and Polygons 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!