MATLAB Answers


how to write a loop to read two columns for a matrix each time

Asked by xueqi
on 25 May 2013

Hi fellows, I have a matrix called tt (50*40) and I need to read two columns from it each time. And this is my code

if true
  %  for i=1:20

but in this way I get the error message is "Subscript indices must either be real positive integers or logicals." Any one know how to solve it? Or do you have another way to read the data as I want?




No products are associated with this question.

2 Answers

Answer by Image Analyst
on 25 May 2013
Edited by Image Analyst
on 25 May 2013
 Accepted answer

Try this:

% Generate some sample data:
tt = randi(3, 200, 3) 
% Get pairs of adjacent, consecutive rows each iteration.
for row = 1 : 2 : size(tt)
	subdata = tt(row:row+1, :)
	% Now do something with subdata.


Answer by xueqi
on 25 May 2013

oh right. It acutally works in this way. I was missing multiplication syntax...


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!