# Array math: The logical indices contain a true value outside of the array bounds.

2 vues (au cours des 30 derniers jours)
Rob le 9 Avr 2023
I have 1-dimensional vectors, x and y. To return the x value corresponding to the maximum y value, I can use:
x(y==max(y))
Cool!
Now I need to do this for several 1-dimensional y vectors. I could make a loop. But how could I use an array instead?
Similarly, I was able to use this function to return multiple amplitudes in a array, 1 for each y vector.
amplitude = abs( max(y) - min(y) )/2;
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponses (1)

KALYAN ACHARJYA le 9 Avr 2023
Modifié(e) : KALYAN ACHARJYA le 9 Avr 2023
The y value can be store in a cell array and use the cellfun to get the result. Here is an example-
#Without Loop
x=[10 12 13 7 0 9];
y={[50 11 9 19 10 49],[30 11 9 19 10 49],[10 11 9 79 10 49]};
dat=cellfun(@(n)x(n==max(n)),y)
dat = 1×3
10 9 7
#Using loop
n=length(y);
dat=zeros(1,n);
for i=1:n
dat(i)=x(y{i}==max(y{i}));
end
dat
dat = 1×3
10 9 7
Avoiding a loop in all cases is no better decision, instead it can be used loops with proper pre-allocation. Use as per your requirements.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by