필터 지우기
필터 지우기

How can i loop a matrix without knowing is size?

조회 수: 5 (최근 30일)
Joana Santos
Joana Santos 2019년 3월 1일
편집: Joana Santos 2019년 3월 1일
Hello!
So, i'm trying to make a loop where i have a matrix to go through and save their elements in a vector. Just note that i don't know the size of the matrix. So how can i do it?
Thanks!
  댓글 수: 2
Star Strider
Star Strider 2019년 3월 1일
Are you somehow prevented from using the size function?
Joana Santos
Joana Santos 2019년 3월 1일
No, i just didn't know how to use :D

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

채택된 답변

Bob Thompson
Bob Thompson 2019년 3월 1일
So, you have two matrices, one outside the loop, and one inside? Which one are you not sure about the size of?
If it is the outside loop you can set the loop bounds to be dependent on the size of the matrix.
for i = 1:size(mat,1) % (mat,1) for rows, (mat,2) for columns and so on
  댓글 수: 6
Bob Thompson
Bob Thompson 2019년 3월 1일
It's your indexing for v. Your i loop only goes from 1:2, so indexing v with (i) will just give you v(1) or v(2). You need to change this to be a combination of i and j.
v((i-1)*size(mat,2)+j) = mat(i,j);
Also, you need to change your dimension your specifying for the innner loop. When you call size() the first input is the matrix, and the second input is the dimension. 1 is rows, 2 is columns, 3 is sheets, etc.
Joana Santos
Joana Santos 2019년 3월 1일
편집: Joana Santos 2019년 3월 1일
Thank you so much! You helped me a lot :)
Have a nice day xx

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by