Daemios
Daemios

Reputation: 105

How to apply the result and index of the result of max() to an array on a single line

I was given a problem in MatLab where I needed to write a single line of code that starts with

variableName =

and find the max value of a 2d array and it's index. I don't use matlab at all, and this seems infuriatingly simple in any language I know. I know to get the value & index of the result of max, you do something like

[M,I] = max(stuffToCheck)

I just dont understand how to assign the array that creates to a variable name. I've spent some time googling but this feels like a very weird constraint so I haven't found anything yet. How do I do this on one line?

Upvotes: 1

Views: 31

Answers (1)

Andrea Bellizzi
Andrea Bellizzi

Reputation: 497

use the variable you want to assign the result:

[variableName(:,1),variableName(:,2)] = max(stuffToCheck)

its the only way because in matlab if you write :

variable = function();

matlab return only the first output, to get other ouput you have to write:

[output1,output2,...] = function();

Upvotes: 3

Related Questions