How to calculate an output from each row of a matrix?

7 visualizaciones (últimos 30 días)
Benjamin
Benjamin el 15 de Abr. de 2024
Comentada: Benjamin el 15 de Abr. de 2024
I have a 5000x5000 matrix A, and I want to calculate the difference between the minimum and maximum of each row as a percentage of the largest value. I already know how to do this for a single row:
percentage = abs((min(A(1000,:))-max(A(1000,:)))./max(A(1000,:)))*100
Unrecognized function or variable 'A'.
The issue I have is that I can't work out how to iterate this function over every row of A. I've tried using an anonymous function:
x = linspace(1:5000)
q = @(x) abs((min(A(x,:))-max(A(x,:)))./max(A(x,:)))*100
q(x)
But this didn't work at all.
Any ideas on how I might iterate the percentage function for every row of the matrix? Also, I need to plot the output of the percentage function against the row number.
Thanks!

Respuesta aceptada

Ayush Modi
Ayush Modi el 15 de Abr. de 2024
Editada: Ayush Modi el 15 de Abr. de 2024
Hi Benjamin,
You can calculate the difference between the minimum and maximum of each row without anonymous function. See below:
minVals = min(A, [], 2); % Minimum of each row
maxVals = max(A, [], 2); % Maximum of each row
percentage = abs((minVals - maxVals) ./ maxVals) * 100;
rowNumbers = 1:5000; % Row numbers
plot(rowNumbers, percentage);
MATLAB performs the "min" and "max" calculation for each row and stores the array in minVals and maxVals.
% Syntax to perform the min and max calculation dimension-wise
min(A,[],dim) % returns the minimum element along dimension dim. For example, if A is a matrix, then min(A,[],2) returns a column vector containing the minimum value of each row.
% Same for max.
Please refer to the following MathWorks documentation for more information on:

Más respuestas (0)

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by