how to not exceed limits of the matrix column

for eg, i have matrix A=[4 6 3 8 9 5;1 7 2 4 3 4;7 8 5 6 1 2; 1 8 4 7 10 6;] and i use a for loop to go across the column, for i =1:col .... how do i stop it before getting error of matrix exceeds dimensions

 채택된 답변

José-Luis
José-Luis 2017년 9월 10일

0 개 추천

your_array = rand(10,16)
%looping through all columns
for ii=your_array
%ii now contains all values in column
end

댓글 수: 4

minion001
minion001 2017년 9월 10일
편집: minion001 2017년 9월 10일
in a for loop, i pick call a function, which picks the most smallest elevation change, but when it goes to the edge of the column it says matrix exceeds dimension, this is my code
function[Rows,Cols]=Elevation(c,A)
[row,col]=size(A);
m=currentposition(1);
n=currentposition(2);
currentposition=[m,n]
i=1;
for i=1:col
[pick]=NewElement(currentposition, A); %this is a function im calling
Rows(i)=currentposition(1);
Cols(i)=currentposition(2);
currentposition=pick;
i=i+1;
end end
from this is get columns 1-5 but once it is on column 6 it gives me matrix exceeds dimension
José-Luis
José-Luis 2017년 9월 10일
편집: José-Luis 2017년 9월 10일
Why do you do i=i+1?
It is unnecessary, if what you want is to increment the iterator index.
Why do you call both loop iterators i?
Call the first one ii and the second one jj or whatever, but definitely not the same name.
minion001
minion001 2017년 9월 10일
i did that but how do i stop getting error when im at column 6 and its searching for elements next to it, where there isn't any elements present, resulting in matrix exceeds dimensions
José-Luis
José-Luis 2017년 9월 10일
Columns 1-5 of what? You are not indexing into A and that is the array whose columns you are looping on.
Please use the debugger. Place a breakpoint in the line that's giving you grief and look at the arrays you are indexing into.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2017년 9월 10일

편집:

2017년 9월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by