For loop to include an array into a different array

2 Ansichten (letzte 30 Tage)
Lindsay Knuth
Lindsay Knuth am 22 Mär. 2018
Beantwortet: Guillaume am 22 Mär. 2018
I have a array that has all permutations for a set of 6 numbers. I need to add it to a different array that has set first and last elements. When I run the function, I get an error message that states "Subscripted assignment dimension mismatch."
nw = 7;
inter= [2 3 4 5 6 7];
interRarr= perms(inter);
Rarr= zeros(factorial(nw-1), nw);
for i=1:factorial(nw-2)
Rarr(i,:) = [1 interRarr(i,:) 1];
end

Akzeptierte Antwort

Lindsay Knuth
Lindsay Knuth am 22 Mär. 2018
I figured it out! Rarr was set up too small instead of it being
Rarr= zeros(factorial(nw-1), nw);
it had to be
Rarr= zeros(factorial(nw-2), nw+1);

Weitere Antworten (1)

Guillaume
Guillaume am 22 Mär. 2018
You're trying to stuff 6+2 = 8 columns in an array of 7 columns. Of course, it's not going to work. An easy fix would be to declare Rarr with 8 columns (i.e. nw+1), but even simpler not to bother with the loop and create Rarr directly with:
Rarr = [repmat(1, size(interRarr, 1), 1), interRarr, repmat(1, size(interRarr, 1), 1)];

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by