Error using horzcat in code trying to get rid of discontinuities

Hi everyone!
When i run this code
(it reads an image rotates it then gets 100 profiles of the same length from the image and then tries to eliminate the discontinuities in the profiles)
img101 = imread('c1=100.800.1.0.1.png');
img_rot= imrotate(img101,32.06);
for i=1:100
prof_img_rot(1:256,i)=img_rot(212+i,400:655)';
end
x_0 = [1 800];
y_0 = [1 501.06];
y = improfile(img101,x_0,y_0)
yOrig = y;
limit = 0.2;
dy = [0, diff(y)];
jump = strfind(abs(dy) > limit, [false, true, false]);
for ijump = 1:numel(jump)
k = jump(ijump);
y(k+1:end) = y(k+1:end) - dy(k+1);
end
figure
axes('NextPlot', 'add')
plot(x, yOrig, '-r', x, y, 'bo')
y2 = lowpass(y, 30, 200);
plot(x, y2, 'c+')
i get the next error:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in lowpass_discontinuities_simple (line 34)
dy = [0, diff(y)];
Does anyone know why?
I have attached the image im reading

 Accepted Answer

This error happens because 0 is a scalar (i.e., it has size 1 in all dimensions) and diff(y) is of size not equal to 1 in some dimension(s) other than dimension 2, which is the dimension of concatenation with horizontal concatenation (i.e., using [] for concatenation), so that 0 and diff(y) cannot be horizontally concatenated. Try this instead:
dy = [zeros(size(y,1)-1,1,size(y,3)), diff(y)];

4 Comments

Thank you so much @Benjamin.
When i run the new code i get this error
Error using strfind
Input strings must have one row.
Error in lowpass_discontinuities_simple (line 35)
jump = strfind(abs(dy) > limit, [false, true, false]);
do you know why?
Actually, what you want to do instead of this (the way you had it):
dy = [0, diff(y)];
is this:
dy = [0; diff(y)].';
Try that and see how it goes.
(Reason: y is a column vector. This does vertical concatenation to prepend a zero element to diff(y), then make the result a row vector so you can use strfind on it. In my answer, it wasn't clear to me what the size or dimensionality of y was, nor was it clear that you actually wanted to do vertical concatentation rather than horizontal concatenation in this case, but the answer would work for doing horizontal concatenation ragardless of the size of y if the dimensionality was not more than three.)

Sign in to comment.

More Answers (0)

Categories

Asked:

on 22 Dec 2021

Commented:

on 22 Dec 2021

Community Treasure Hunt

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

Start Hunting!