Calculate values inside a nested loop with different steps and store them in the same variable.
11 visualizaciones (últimos 30 días)
Ancalagon8 el 8 de Dic. de 2022
I have a matrix (x) and want to calculate where its values are above certain thresholds and store the results in a new matrix (C). To be more specific, I need to check whether the condition sum(x>i) is met in 18 different values of i (classes). My difficulty is that I want a different step so for the first ten values step=1, while the remaining 8 while have step=5. I tried a nested loop but the result was not the expected. Any ideas how to proceed?
Stephen23 el 16 de Dic. de 2022
Editada: Stephen23 el 16 de Dic. de 2022
Assuming that SUM() returns a scalar:
V = [1:10,15:5:50];
C = V; % preallocate
for k = 1:numel(V) % loop over indices, not your data values
C(k) = sum(x>V(k));
Note that in MATLAB it is almost always easier to iterate over indices, rather than over data values.
C = arrayfun(@(v)sum(x>v), V)