Dont see why this code doesnt work
Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Ältere Kommentare anzeigen
for i=1:11
x = z(i)/0.9;
pipeLength = sqrt(x.^2+z.^2);
if pipeLength <= 10
pipeCost(i) = price1*pipeLength
else
pipeCost(i) = price1*10 + price2*(pipeLength-10); % Price for lengths greater than 10 m
end
end
My program works fine up until this point.
I am trying to compute the cost of pipe depending on the length. Lengths up to 10 feet are 25 while anything after is 15. When I run the program it gives me the error,
In an assignment A(I) = B, the number of elements in B and I must
be the same.
Error in waterpipe (line 34)
pipeCost(i) = price1*10 + price2*((pipeLength)-10); % Price for
lengths greater than 10 m
any ideas?
Antworten (3)
Azzi Abdelmalek
am 10 Feb. 2014
Bearbeitet: Azzi Abdelmalek
am 10 Feb. 2014
Use pipeCost(i,:) instead of pipeCost(i)
z=rand(1,11)
price1=1;
price2=2
for i=1:11
x = z(i)/0.9;
pipeLength = sqrt(x.^2+z.^2);
if pipeLength <= 10
pipeCost(i,:) = price1*pipeLength
else
pipeCost(i,:) = price1*10 + price2*(pipeLength-10);
end
end
Image Analyst
am 10 Feb. 2014
When you say this
pipeLength = sqrt(x.^2+z.^2);
x is a scalar (single number), but z is an array. So pipeLength is an array of several numbers. Which means price1*10 + price2*(pipeLength-10) is also an array of several numbers. However you are trying to stick that array into a single element of pipeCost. Can't do that. It can be a whole row of pipeCost if you make pipeCost a 2D array, or make pipeLength a scalar:
pipeLength = sqrt(x.^2+z(i).^2);
2 Kommentare
Image Analyst
am 10 Feb. 2014
No. Did you not read my detailed explanation? Again, it doesn't like you stuffing a bunch of numbers into an array element that can take only a single number.
Azzi Abdelmalek
am 10 Feb. 2014
Jason, Edit your question or add a comment to any answer of your choice by clicking on comment on this answer
Diese Frage ist geschlossen.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!