Is it possible to make this if statement more concise?

34 visualizaciones (últimos 30 días)
Thomas
Thomas el 16 de Nov. de 2024 a las 21:10
Editada: Image Analyst el 16 de Nov. de 2024 a las 23:02
Hello,
I have an assignment that asks me to:
  1. Define V1, V2, V3 using linspace, colon operator, and [list]
  2. Use length command to find and store the lengths of the three vectors in one vector
  3. Use max command to determine the max length of the three vectors.
  4. Display the maximum length nicely as an integer using fprintf.
I believe I have done 1 through 3 properly and I am working on 4. Instead of hardcoding the longest vector into fprintf I wanted to make an IF statement that determines which vector is the longest and creates a string saying so. I was wondering if there is a better way to formate the statement than what I have come up with. Please keep in mind that I am new so while I am open to all suggestions, I might not understand your answer if it is complex (please explain it a little bit).
Thanks,
% create three row vectors using linspace, colon operator, and [list]
V1 = linspace(2,53,84);
V2 = 1:2.3:48;
V3 = [1, 42, 5, 5, 333, 33, 2.4, 5.4, 69];
% Use length command to find and store the lengths of the three vectors
V_length = [length(V1), length(V2), length(V3)];
% Use max command to determine the max length of the three vectors.
V_max = max(V_length);
% If statement to determine which vector is longest and output a string
% so if vectors change length fprintf changes accordingly
if V_max == length(V1)
if V_max == length(V2) && V_max == length(V3)
long_V = "all vectors same length";
elseif V_max == length(V2)
long_V = "V1 and V2";
elseif V_max == length(V3)
long_V = "V1 and V3";
else
long_V = "V1";
end
elseif V_max == length(V2)
if V_max == length(V3)
long_V = "V2 and V3";
else
long_v = "V2";
end
elseif V_max == length(V3)
long_V = "V3";
else
warning('Check V_max')
end
% Display the maximum length nicely as an integer with context words.
fprintf(['The vector(s) with the most number of elements ' ...
'out of V1, V2, and V3, are: %s. \n' ...
'The length of %s is: %d \n'], long_V, long_V, V_max)

Respuestas (1)

Image Analyst
Image Analyst el 16 de Nov. de 2024 a las 21:28
Try
% Create three row vectors using linspace, colon operator, and [list]
V1 = linspace(2,53,84);
V2 = 1:2.3:48;
V3 = [1, 42, 5, 5, 333, 33, 2.4, 5.4, 69];
% Use length command to find and store the lengths of the three vectors
V_length = [length(V1), length(V2), length(V3)]
V_length = 1×3
84 21 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Use max command to determine the max length of the three vectors.
[V_max, indexOfLongest] = max(V_length)
V_max = 84
indexOfLongest = 1
vectorNames = {'V1', 'V2', 'V3'};
fprintf('The longest vector is %s with a length of %d.\n', vectorNames{indexOfLongest}, V_max);
The longest vector is V1 with a length of 84.
  3 comentarios
Image Analyst
Image Analyst el 16 de Nov. de 2024 a las 23:02
Editada: Image Analyst el 16 de Nov. de 2024 a las 23:02
Your lengths were 84, 21, and 9. Look at the output in my answer. So V1 is the longest vector. If you have multiple vectors that may be the same length, then the code will have to be adapted for that. Like (untested)
[V_max, indexOfLongest] = max(V_length)
can go to
V_max = max(V_length)
indexOfLongest = find(V_length == V_max)
and then have a for loop to loop over all values of indexOfLongest
for k = 1 : length(indexOfLongest)
thisIndex = indexOfLongest(k);
fprintf('The longest vector is %s with a length of %d.\n', vectorNames{thisIndex}, V_max(thisIndex));
end
To learn about cell arrays and when to use braces, brackets, and parentheses, see the FAQ: What is a cell array

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by