I'm trying to make a cycle that would process a section of the vector and return this section to me changed
x=rand(1,2302)
y=rand(1,2302)
step=100;
for i=1:size(x,2)
i1=y(i:i+step);
i2=x(i:i+step);
a(i)=myfun(i1,i2);
end
%% for example my function
function [m,s] = myfun(x,y)
n = length(x);
m = x-0.26;
s= y-0.77
end
thank you in advance

댓글 수: 2

Please share the code for myfun.
Lev Mihailov
Lev Mihailov 2022년 4월 4일
@Davide Masiello I added an example function

댓글을 달려면 로그인하십시오.

 채택된 답변

Davide Masiello
Davide Masiello 2022년 4월 4일

0 개 추천

The problem is myfun spits out two vectors of size 1 x step+1.
You could assign this vectors to the rows of a new matrix (see a and b in the example below) .
x = rand(1,2302);
y = rand(1,2302);
step = 100;
for i = 1:size(x,2)-step
i1 = y(i:i+step);
i2 = x(i:i+step);
[a(i,:),b(i,:)] = myfun(i1,i2);
end
function [m,s] = myfun(x,y)
n = length(x); % <--- not needed
m = x-0.26;
s= y-0.77;
end

댓글 수: 3

@Davide Masiello Thank you, but I have a question, could you help me, if in my function the values ​​are reversed (1:size(x) to 1:size(x),1) how can I get back a vector of length 1:size(x)
%%% somewhere in my function
s=s';
In that case you can replace
[a(i,:),b(i,:)]
with
[a(:,i),b(:,i)]
so each output of myfun is stored as a column in the matrices a and b.
Lev Mihailov
Lev Mihailov 2022년 4월 4일
@Davide Masiello just now I noticed this, and my function returns me the same vector (but changed), but why is it a matrix and not a vector?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

태그

질문:

2022년 4월 4일

댓글:

2022년 4월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by