필터 지우기
필터 지우기

How can I create a multidimensional array from two vectors that have different dimensions using a for loop?

조회 수: 8 (최근 30일)
Hi I’m trying to create the following matrix from two vectors
A=1:2
B=4:7
C=[
1 4
1 5
1 6
1 7
2 4
2 5
2 6
2 7]
Using a for loop So far I have
A=1:2
B=4:7
For i=1:2
For j=1:4
C(i,j)= [A(i) B(j)]
End
End
After that I get a dimensional error. Any suggestions?
  댓글 수: 2
Guillermo Naranjo
Guillermo Naranjo 2018년 9월 14일
The arrays I have are 56 elements by 250. So I figured a for loop would be faster. Is there an easier way? Thanks for your answers!

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

채택된 답변

Matt J
Matt J 2018년 9월 13일
for i=1
[b,a]=ndgrid(B,A);
C=[a(:),b(:)]
end
  댓글 수: 1
Guillermo Naranjo
Guillermo Naranjo 2018년 9월 14일
Thanks! this worked perfectly. I guess the need for the For loop is futile at this point. :) I was not aware of the ndgrid function. I appreciate the help!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by