How can I find the maximum value and its index in array in MATLAB?
The function is max. To obtain the first maximum value you should do [val, idx] = max(a); val is the maximum value and idx is its index.
The function is max. To obtain the first maximum value you should do [val, idx] = max(a); val is the maximum value and idx is its index.
I think the sortrows function is what you’re looking for. >> sortrows(data,1) ans = -1 4 1 3 5 7
To shuffle the rows of a matrix, you can use RANDPERM shuffledArray = orderedArray(randperm(size(orderedArray,1)),:); randperm will generate a list of N random values and sort them, returning the second output of sort as result.
MATLAB Compiler encrypts and archives your MATLAB code (which remains as MATLAB .m code), and packages it in a thin executable (either .exe or .dll) wrapper. This is delivered to the end user along with the MATLAB Compiler Runtime (MCR). If you wish, the MCR can be packaged within the executable as well. The MCR … Read more
I usually use EYE for that: A = magic(4) A(logical(eye(size(A)))) = 99 A = 99 2 3 13 5 99 10 8 9 7 99 12 4 14 15 99 Alternatively, you can just create the list of linear indices, since from one diagonal element to the next, it takes nRows+1 steps: [nRows,nCols] = size(A); … Read more
The subaxis function on the File Exchange allows you to specify margins for subplots. Example usage: t = 0:0.001:2*pi+0.001; figure(2); for i = 1 : 25; subaxis(5,5,i, ‘Spacing’, 0.03, ‘Padding’, 0, ‘Margin’, 0); plot(t, sin(i*t)); axis tight axis off end
My answer to this is the same as in an answer to your earlier question. For a probability density function, the integral over the entire space is 1. Dividing by the sum will not give you the correct density. To get the right density, you must divide by the area. To illustrate my point, try … Read more
The original code you suggest is the best way. Matlab is extremely good at vectorized operations such as this, at least for large vectors. The built-in norm function is very fast. Here are some timing results: V = rand(10000000,1); % Run once tic; V1=V/norm(V); toc % result: 0.228273s tic; V2=V/sqrt(sum(V.*V)); toc % result: 0.325161s tic; … Read more
mfilename or better mfilename(‘fullpath’)
You can create this sort of plot yourself pretty easily using the built-in functions imagesc and text and adjusting a number of parameters for the graphics objects. Here’s an example: mat = rand(5); % A 5-by-5 matrix of random values from 0 to 1 imagesc(mat); % Create a colored plot of the matrix values colormap(flipud(gray)); … Read more