Asked by Debanjan Borthakur
on 10 Jun 2019

I am using the function [Xa,Ya] = alignsignals(X,Y). Now I want to perform this operation on an arry of X and Y's and store them on Xa's and Ya's. Any suggestions. Here X and Y will be a matrix instead of a single vector.

Answer by KALYAN ACHARJYA
on 10 Jun 2019

Edited by KALYAN ACHARJYA
on 10 Jun 2019

%I assumed both (X & Y) have same lengths

for i=1:lengths(x)

[Xa(i),Ya(i)]=alignsignals(X(i),Y(i));

end

For example

y1=[1 2 3 4];

y2=[4 5 6 7];

x=[];

y=[];

for i=1:length(y1)

[x(i) y(i)]=fun4(y1(i),y2(i));

end

fun4

function [x y]=fun4(a,b)

x=a+b;

y=a-b;

end

If I misundestood your question, let me know.

Debanjan Borthakur
on 10 Jun 2019

Here is my code which does not work :

d1=resample(data,25,200); %% here d1 is my matrix and I want to align signal1 with each of the columns of d1.

for i=1:size(d1,2)

%[Xa(i),Ya(i)]=alignsignals(X(i),Y(i));

[xx(i),yy(i)]=alignsignals(signal1(i) ,d1(:,i));

end

KALYAN ACHARJYA
on 10 Jun 2019

@Debanjan data?

Debanjan Borthakur
on 10 Jun 2019

data is just a matrix (2961*258)

Sign in to comment.

Answer by KSSV
on 10 Jun 2019

[m,n] = size(X) ;

Xa = zeros([],m) ;

Ya = zeros([],n) ;

for i = 1:n

[xa,ya] = alignsignals(X(:,i),Y(:,i)) ;

Xa(:,i) = xa ;

Ya(:,i) = ya ;

end

If function is shown...may be it can be vectorised and work for entire matrix.

Debanjan Borthakur
on 10 Jun 2019

hi, your first line results error : Too many output arguments.

Debanjan Borthakur
on 10 Jun 2019

Here is my code which does not work :

d1=resample(data,25,200); %% here d1 is my matrix and I want to align signal1 with each of the columns of d1.

for i=1:size(d1,2)

%[Xa(i),Ya(i)]=alignsignals(X(i),Y(i));

[xx(i),yy(i)]=alignsignals(signal1(i) ,d1(:,i));

end

Debanjan Borthakur
on 10 Jun 2019

d1=matrix (2961*258)

n=258

m=n

Xa = zeros([],m) ;

Ya = zeros([],n) ;

for i = 1:n

[Xa(1:length(M_1norm),i),Ya(:,i)] = alignsignals(M_1norm,d1(1:length(M_1norm),i)) ;

end

Unable to perform assignment because the size of the left side is 0-by-1 and the size of the right side is 3292-by-1.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Debanjan Borthakur (view profile)

Direct link to this comment:https://in.mathworks.com/matlabcentral/answers/466250-using-for-loop-in-alignsignal-function#comment_713156

Sign in to comment.