How to calculate a max value in a array without the built in functions.

32 ビュー (過去 30 日間)
Giuseppe
Giuseppe 2014 年 3 月 12 日
The array is
Time = [1,6,3,6,8,12,1]
I think you need to use a for or while loop. There is no predetermined max value however as it is time.
Thanks.

採用された回答

Mischa Kim
Mischa Kim 2014 年 3 月 12 日
編集済み: Mischa Kim 2014 年 3 月 12 日
Giuseppe, a for-loop would be the way to go:
maxval = Time(1);
for ii = 1:length(Time)
if Time(ii) > maxval
maxval = Time(ii);
end
end
Alternatively, you could use max(Time).
  4 件のコメント
Steven Lord
Steven Lord 2020 年 4 月 21 日
Of course it does. But the terms of the homework assignment (which Mischa's solution technically violates, as > is a built-in function) prohibit the use of built-in functions. This information is in the title of the original question, not the body. [Yes, technically the question doesn't state this was a homework assignment, but I've been on Answers and the MATLAB newsgroup before that for long enough to know a homework question when I read it.] Two such functions:
Time = [1,6,3,6,8,12,1];
[maxValue, maxLocation] = max(Time)
[maxValue2, maxLocation2] = maxk(Time, 1)
If you're willing to allow a bit more work:
[sortedTime, sortedIndices] = sort(Time, 'descend');
maxValue3 = sortedTime(1)
maxLocation3 = sortedIndices(1)
If Time were an array rather than a vector and you're using release R2018b or later of MATLAB, you can use max. Though if you want the linear index second output, you need to use release R2019a or later.
TimeA = reshape(randperm(4*5*6), [4 5 6]);
maxValue4a = max(TimeA, [], 'all')
[maxValue4b, maxLocation4b] = max(TimeA, [], 'all', 'linear')
I suspect the goal of the assignment was for the students to practice using loops to walk through the elements of an array, processing each element in turn as Mischa's solution does.
manikanth Jonnadula
manikanth Jonnadula 2022 年 4 月 29 日
Hey mate, can you help me how to find the Max Array from 5 identical Datasets using double For Loops please.

サインインしてコメントする。

その他の回答 (2 件)

Shivani Dixit
Shivani Dixit 2021 年 6 月 1 日
You can use for loop or while loop for finding out the maximum or minimum element in the array without using any built-in functions.
Finding out maximum value in an array using for loop is shown below:
Time = [1,6,3,6,8,12,1];
ans = Time(1); % Initially take maximum element as first element and after iterating over the loop we will get final answer
for i=1:length(Time)
if(Time(i)>ans)
ans=Time(i);
end
end
% The variable 'ans' would store the final maximum value

Carlos
Carlos 2014 年 3 月 12 日
編集済み: Carlos 2014 年 3 月 12 日
A very easy newbie way to do it using a while loop (better with a for loop as Mischa states)
>> Time = [1,6,3,6,8,12,1];
>> max=Time(1);k=1;
>> while(k<=length(Time))
if(Time(k)>max)
max=Time(k);
end
k=k+1;
end
  3 件のコメント
Carlos
Carlos 2014 年 3 月 12 日
I think you misunderstood what I have stated. I have pointed out it is a better practice to do this with a for loop(please read my answer again), but Giuseppe mentioned the possibility of implementing it with a while loop and that is the reason I posted the code with a while loop.
Giuseppe
Giuseppe 2014 年 3 月 14 日
thank you this is still useful. now i understand more about while vs for loops.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by