Is it possible to make this if statement more concise?
34 views (last 30 days)
Show older comments
Hello,
I have an assignment that asks me to:
- Define V1, V2, V3 using linspace, colon operator, and [list]
- Use length command to find and store the lengths of the three vectors in one vector
- Use max command to determine the max length of the three vectors.
- 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)
0 Comments
Answers (1)
Image Analyst
on 16 Nov 2024 at 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)]
% Use max command to determine the max length of the three vectors.
[V_max, indexOfLongest] = max(V_length)
vectorNames = {'V1', 'V2', 'V3'};
fprintf('The longest vector is %s with a length of %d.\n', vectorNames{indexOfLongest}, V_max);
3 Comments
Walter Roberson
on 16 Nov 2024 at 22:15
Image Analyst
on 16 Nov 2024 at 23:02
Edited: Image Analyst
on 16 Nov 2024 at 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
See Also
Categories
Find more on Matrix Indexing 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!