How to Loop through a matrix/array

I have a matrix/array that is 804 X 1. I want to be able to use a for loop to take the first 58 values put them in array a1, then take the next 58 values and place them in array a2 and so on. How would I do this?

답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 11일

0 개 추천

If you want a3 just use
n=3
idx1=(n-1)*58+1
idx2=n*58
out=a(idx1:idx2)

댓글 수: 1

Adam
Adam 2013년 3월 11일
I want to take the first 58 values in matrix A that is (804X1) and put them in a1. Then take the second 58 values at place them in a2 and some on until I run through matrix A

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

Azzi Abdelmalek
Azzi Abdelmalek 2013년 3월 11일
편집: Azzi Abdelmalek 2013년 3월 11일

0 개 추천

a=rand(1,804) %example
m=numel(a);
n=ceil(m/58);
r=n*58-m;
for k=1:n-1
idx1=(k-1)*58+1;
idx2=k*58;
assignin('base',sprintf('a%d',k),a(idx1:idx2))
end
assignin('base',sprintf('a%d',k+1),a(idx2+1:end))

댓글 수: 1

Adam
Adam 2013년 3월 11일
Ok better So I have a matrix called Mw_12 that is (804X1). I need to take the first 58 values find the max value reduce it by 15% and then store it back in a matrix a1. example code a1=Mw_12([1:58]); dailymax = max(a1)*.85; a1(a1>dailymax)=dailymax;
Then at the end I want to combine a1 thru a# to a NEW_Mw_12[a1;a2;....;a#]

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

카테고리

도움말 센터 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2013년 3월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by