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

1 次查看(过去 30 天)
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;

回答(1 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2023-4-9
编辑:KALYAN ACHARJYA 2023-4-9
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.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by