# How can I assign a variable to all the columns and all the rows of a matrix?

조회 수: 59(최근 30일)
Mau Dudas 2019년 10월 23일
답변: Jos (10584) 2019년 10월 23일
Got a 20x10 double matrix and I'd like to assign 'numsub' to the number of rows and 'numtrials' to the number of columns in the whole matrix so that I can use them as an index in a for loop for extracting values from certain subjects.
I will need to do something like this:
for i = 1:numsubs
disp(['sub' num2str(i)])
disp('Slow trial')
for j=1:numtrials
if RT(j,i)<=0.3
disp(['trial' num2str(j)])
end(x3)

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

### 채택된 답변

Jos (10584) 2019년 10월 23일
Are you looking for the function SIZE?
data = rand(20,10) ;
[numsubs, numtrials] = size(data)

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

### 추가 답변(1개)

Bob Thompson 2019년 10월 23일
You don't need to actually create a new variable for this, you can just use the results from size.
A = randi(100,20,10);
[r,c] = size(A); % Where r is the number of rows, and c is the number of columns
% You can also just put the function directly into your loop
for i = 1:size(A,1)
...
for j = 1:size(A,2)
...
end
end

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

### Community Treasure Hunt

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

Start Hunting!