Matrix dimensions must agree.

4 views (last 30 days)
Olu'Segun Oseni
Olu'Segun Oseni on 14 Nov 2017
Answered: Walter Roberson on 15 Nov 2017
I am using an array value
FPR = [1.0:0.001:2.0];
n = length(FPR);
to give values into a for loop
for i = [1:1:n]
FPR(i) = (i);
in the for loop i use an if statement to interpolate an answer from a graph and it uses an array. I know that T03 and CombTD have become vectors but i dont know how to get my if statement to work for vectors and not elements. This is the code.
a = [300,400,500,600,700,800,900];
TempInlet = T03;
TempDiff = CombTD;
LSearch = find(a <= TempInlet,1,'last');
HSearch = find(a >= TempInlet,1,'first');
lower = a(LSearch);
higher = a(HSearch);
if lower>=300 && higher<=400
xlower = TempDiff/37076.92308;
yhigher = TempDiff/36230.76923;
elseif lower>=400 && higher<=500
xlower = TempDiff/36230.76923;
yhigher = TempDiff/35461.538461;
elseif lower>=500 && higher<=600
xlower = TempDiff/35461.538461;
yhigher = TempDiff/34615.384615;
elseif lower>=600 && higher<=700
xlower = TempDiff/34615.384615;
yhigher = TempDiff/33769.230769;
elseif lower>=700 && higher<=800
xlower = TempDiff/33769.230769;
yhigher = TempDiff/33230.76923;
elseif lower>=800 && higher<=900
xlower = TempDiff/33230.76923;
yhigher = TempDiff/32769.230769;
end
percentage = (TempInlet-lower)/100;
FAR = xlower+((yhigher-xlower)*percentage)
and this is the error code
Matrix dimensions must agree.
Error in Looptest (line 100)
LSearch = find(a <= TempInlet,1,'last');
  1 Comment
KSSV
KSSV on 15 Nov 2017
Give either dimensions or values of the variables......so that we can run the code...

Sign in to comment.

Answers (2)

N.
N. on 15 Nov 2017
The error is in these lines:
LSearch = find(a <= TempInlet,1,'last');
HSearch = find(a >= TempInlet,1,'first');
if a and TempInlet have not the same size, you cannot compare them. What do you want to do exactly ? What is "lower" and "higher" ?

Walter Roberson
Walter Roberson on 15 Nov 2017
For vector TempInlet, you can replace
LSearch = find(a <= TempInlet,1,'last');
HSearch = find(a >= TempInlet,1,'first');
with
[~, LSearch] = histc(TempInlet, [a, inf]);
HSearch = LSearch;
mask = TempInlet ~= a(LSearch);
HSearch(mask) = HSearch(mask) + 1;
The mask will usually be true: it will only be false in the places where the temperatures are exactly on the boundary.
This is a translation of your code. However, I do not think you need HSearch or xlower or xupper and all those if statements. I think you should use just a small number of lines:
temp_divs = [37076.92308, 36230.76923, 35461.538461, 34615.384615, 33769.230769, 33230.76923, 32769.230769];
xlower = TempDiff ./ temp_divs(LSearch);
xupper = TempDiff ./ temp_divs(LSearch+1);

Categories

Find more on MATLAB 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!