# Arrays have incompatible sizes for this operation.

104 views (last 30 days)
BP on 5 Oct 2021
Answered: Jan on 6 Oct 2021
I have the for loop shown below. It is giving me this error:
"Arrays have incompatible sizes for this operation.
Error in EMP_ASCO_Waveform_Analyzer_9_15_r2 (line 186)
xm(k1) = interp1(y(idxrng2+(-3:3)), x(idxrng2+(-3:3)), hafmax(k1));
Related documentation "
Thanks
--------------------------------------
for k1 = 1:numel(hafmax)% Finds the halfmax rise and fall numbers
idxrng1 = find(y(1:idx)<hafmax(:,k1), 1, 'last');
idxrng2 = find(y(idx:numel(x))<hafmax(:,k1),1,'first')+idx;
xm(k1) = interp1(y(idxrng1+(-3:3)), x(idxrng1+(-3:3)), hafmax(k1));
xm(k1) = interp1(y(idxrng2+(-3:3)), x(idxrng2+(-3:3)), hafmax(k1));
end

Jan on 6 Oct 2021
Use the debugger to identify the problem. Type this in the command window:
dbstop if error
and run the code again. When Matlab stops at the error, check the sizes of the arrays. I guess it is here:
idxrng1 = find(y(1:idx)<hafmax(:,k1), 1, 'last');
size(y(1:idx))
size(hafmax(:,k1))
There might be another problem:
for k1 = 1:numel(hafmax)
idxrng1 = find(y(1:idx)<hafmax(:,k1), 1, 'last');
If hafmax is not a vector, counting the 2nd index until the number of all elements must fail. If it is a vector, write "hafmax(k1)", which is nicer and even faster.
Hint: y(idxrng1+(-3:3)) is slower than y(idxrng1-3:idxrng1+3). In the later case only the first and last index is checked to be inside the valid range, while in the first case this test is performed for each index. If the number of indices is 7, the difference is not tragic, but for larger index vectors is helps to reduce the runtime.

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by