Vector not updating in while loop

The user will enter the length of several pipes as a vector (all positive integers). You must cut all the pipes into pieces using as few cuts as possible, but you can only make cuts the length of the smallest pipe left.
It only subtracts the minimum value from the original vector once and then it just continues counting without updating my vector.
pipe = input('Enter pipe lengths in []: ');
cut = 0;
while max(pipe) > 0
pipe = pipe - min(pipe);
cut = cut + 1;
end
fprintf('%g cuts are needed!',cut);

Antworten (1)

Cris LaPierre
Cris LaPierre am 13 Okt. 2021

0 Stimmen

It is replacing the current value of pipe with the new value. It sounds like you instead want to create a vector. See Ch 13 of MATLAB Onramp for you to create a vector using a for loop.
You might also consider borrowing from the examples on the for documentation page.

Kategorien

Mehr zu Fluid Network Interfaces Library finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2021a

Gefragt:

am 13 Okt. 2021

Beantwortet:

am 13 Okt. 2021

Community Treasure Hunt

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

Start Hunting!

Translated by