How to call the rows of a matrix without the for loop

조회 수: 2 (최근 30일)
Waheeb Butt
Waheeb Butt 2017년 12월 12일
댓글: Waheeb Butt 2017년 12월 12일
Hi I have a matrix A=[1 2 3 4 5;6 7 8 9 10; 11 12 13 14 15] and I want to call each row of the matrix one by one as an input to my program. i.e, A(1,:) is taken as first input, A(2,:) is the second input and finally A(3,:), just like it is done in a loop, but is there any way that I can do the same without using a for loop.
Any help would be much appreciated.
  댓글 수: 1
Fangjun Jiang
Fangjun Jiang 2017년 12월 12일
Do you want MyFun(A(1,:),A(2,:),A(3,:)), or MyFun(A(1,:)), MyFun(A(2,:)), MyFun(A(3,:))? How do you plan to do it using for-loop?

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

채택된 답변

KL
KL 2017년 12월 12일
Probably convert them to cell array and then the use of comma-separated lists,
B = num2cell(A,2)
and then
your_program(B{:})
  댓글 수: 3
KL
KL 2017년 12월 12일
편집: KL 2017년 12월 12일
...for further processing in your_program...
if you're talking about handling them inside your_program, it's pretty normal like,
function out = your_program(a,b,c)
x = a(1);
...
or if you are talking about indexing only specific elements while calling your_program, you should create your cell array with those elements.
Waheeb Butt
Waheeb Butt 2017년 12월 12일
Thanks a lot, Appreciated :)

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

추가 답변 (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