looping through columns of a matrix and saving output

조회 수: 2 (최근 30일)
C.G.
C.G. 2021년 10월 5일
편집: C.G. 2021년 10월 5일
I have a matrix which is 1000x4. Each column is a different experiment run.
I want to loop through each column of the matrix and reshape it by taking the mean of every 1000 values.
for a = 1:4;
GTsec = mean(reshape(vel(1:1000),10,[]),1); %reshape to per second by taking the mean GT over 1000 files
end
  댓글 수: 2
Willie Smit
Willie Smit 2021년 10월 5일
What is the size of vel?
It is not clear from the code, but it seems like you want to calculate the mean, standard deviation, minimum and maximum for different experiments. If each column of vel stores the data of one experiment, then the commands
mean_GT=mean(vel);
std_GT=std(vel);
will return row vectors, where each entry corresponds to an experiment.
C.G.
C.G. 2021년 10월 5일
편집: C.G. 2021년 10월 5일
ok I understand, thanks. I said vel is a 1000x4 double; 4 columns, 1000 entries.
The main thing I want to do is I want to reshape each column and create a new matrix of the reshaped data.
I can do one column of this using the code below but every time it overwrites the previous and I dont want it to.
GTsec = mean(reshape(vel(1:1000),10,[]),1);

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

답변 (1개)

KSSV
KSSV 2021년 10월 5일
This kind of error occurs when you are trying to save more number of elements than it is intiliazed.
Example:
A = zeros(1,3) ;
A(1) = rand ; % no error, becuase you are saving one element as initilaized
A(2) = rand(1,2) ; % error becuase you are saving two elements instead of one
Unable to perform assignment because the left and right sides have a different number of elements.
So you are trying to save RHS which has multiple elements into LHS which is initialized as a single element. To avoid the error, get the dimensions in prior and initialize properly and save. If not save them into a cell array.
A = cell(1,2) ;
A{1} = rand ;
A{2} = ran(1,3) ;
  댓글 수: 1
C.G.
C.G. 2021년 10월 5일
Ok I understand the error, thank you.
I am trying to reshape each column and create a new matrix of the reshaped data.
I can do one column of this using the code below but every time it overwrites the previous and I dont want it to, thats why I was trying to use a loop and this is the line it errors on.
GTsec = mean(reshape(vel(1:1000),10,[]),1);

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by