2017-04-26 57 views
0

我有一個150 X 4矩陣,我希望遍歷矩陣長度並打印出每一行。打印/存儲矩陣中的每一行 - MATLAB

這是我嘗試代碼:

X = xlsread('filename.csv'); 

J = X(:, [2:5]) % extracting rows 2 to 5 into a matrix 

for i= 0:length(J) 
Y = J(i,:); %loop through each row and store it in Y 
end; 

但我不斷收到以下錯誤:

Subscript indices must either be real positive integers or logicals. 

是我的做法不正確?我在這裏錯過了什麼?我只是想遍歷每一行並將其存儲在一個變量中。

回答

1

在MATLAB指數爲1,而不是從0開始,所以你應該做的:

for i= 1:length(J) 
    Y = J(i,:); %loop through each row and store it in Y 
end; 

此外,對於以下行,你寫道:

J = X(:, [2:5]) % extracting rows 2 to 5 into a matrix 

注意,你實際上存儲在J欄2,3,4,5,X而不是2,3,4,5行。

+0

是的,這是我的意圖,我需要將J列2,3,4,5存儲到J中,然後遍歷J中的每一行。您提到的有關索引的內容似乎起作用了,從我的結尾看起來很愚蠢,謝謝! – 221b

+0

不客氣:) –