필터 지우기
필터 지우기

Simple Matrix operation using for loop

조회 수: 1 (최근 30일)
Nikhil
Nikhil 2013년 10월 3일
댓글: Azzi Abdelmalek 2013년 10월 3일
Hello all, I want to do following matrix operation, I have two matrices y and errorsample as: y=y=[0.53, 0.6, 0.58, 0.68, 0.85, 0.85];1X6 matrix errorsample=normrnd(0,stdev,1,1e4);1X1e4 matrix
Now I want to create new matrix Y (1e4X6) such that Y(i)(j)=y(j)-errorsample(i)
For that I have written following code: y=[0.53, 0.6, 0.58, 0.68, 0.85, 0.85]; stdev=0.0523; errorsample=normrnd(0,stdev,1,1e4); for i=1:1:1e4; for j=1:1:6; Y(i)(j)=y(j)-errorsample(i); end end
but after running this, I am getting following error: ??? Error: File: Q4.m Line: 23 Column: 5 ()-indexing must appear last in an index expression.
Can anybody please tell me how to debug this problem, It's urgent Thanks in advance,
Nikhil

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 10월 3일
편집: Azzi Abdelmalek 2013년 10월 3일
Maybe you want
y=[0.53, 0.6, 0.58, 0.68, 0.85, 0.85];
stdev=0.0523;
errorsample=normrnd(0,stdev,1,1e4);
out=zeros(1e4,6);
for i=1:1:1e4;
for j=1:1:6;
out(i,j)=y(j)-errorsample(i);
end
end
disp(out)
  댓글 수: 2
Nikhil
Nikhil 2013년 10월 3일
편집: Azzi Abdelmalek 2013년 10월 3일
Hey, Thanks a lot for your quick reply. My next stage is I want to perform regression analysis on each row matrix Y. And I want to store the answers of each regression analysis on matrix form. generally output of regress command is two vectors my code for this is as follows:
x=[0,0.2, 0.4, 0.6, 0.8, 1.0];
errorsample=normrnd(0,stdev,1,1e4);
for i=1:1:1e4;
for j=1:1:6;
Y(i,j)=y(j)-errorsample(i);
end
end
xx=[ones(6,1),x'];
for i=1:1:1e4;
b=regress(Y(i,:)',xx);
Z(i)=(b);
end
The error which I am getting is as follows: ??? In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in ==> Q4 at 29 Z(i)=(b);
kindly help me to debug this, Thanks in advance,
Nikhil
Azzi Abdelmalek
Azzi Abdelmalek 2013년 10월 3일
y=[0.53, 0.6, 0.58, 0.68, 0.85, 0.85];
stdev=0.0523;
errorsample=normrnd(0,stdev,1,1e4);
Y=zeros(1e4,6);
for i=1:1:1e4;
for j=1:1:6;
Y(i,j)=y(j)-errorsample(i);
end
end
x=[0,0.2, 0.4, 0.6, 0.8, 1.0];
xx= [ones(6,1),x'];
Z=zeros(1e4,2);
for i=1:1:1e4;
b=regress(Y(i,:)',xx);
Z(i,:)=b';
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by