Filter löschen
Filter löschen

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

8 Ansichten (letzte 30 Tage)
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?

Akzeptierte Antwort

Matt J
Matt J am 13 Sep. 2018
for i=1
[b,a]=ndgrid(B,A);
C=[a(:),b(:)]
end
  1 Kommentar
Guillermo Naranjo
Guillermo Naranjo am 14 Sep. 2018
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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Multidimensional Arrays finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by