tabular setdiff with for loop

Is it possible to use a for loop to get multiple outputs of tabular setdiff? ie. This works:
A=table([1;2;3])
% B=1x5 cell with 5 tables to compare to A
D=setdiff(A,B{1,1})
but this doesn't:
for i=1:5
D{i}=setdiff(A,B{i})
end
and gives the error: "You cannot subscript a table using linear indexing (one subscript) or multidimensional indexing (three or more subscripts). Use a row subscript and a variable subscript."
Note:
for i=1:5
D=setdiff(A,B{i})
end
..works too, although it obviously overwrites D with each for loop. How do I make it save the 5 tables to a 1x5 cell like the usual for loop output?

3 Kommentare

Walter Roberson
Walter Roberson am 27 Okt. 2017
Did you possibly have a D table hanging around in memory at the time you tried the D{i}= version ?
UQFG
UQFG am 27 Okt. 2017
Ah... yes, from trying to fix my code. Deleted it and ran the code again, and it now works. Thank you!
Rik
Rik am 27 Okt. 2017
That is one of the reasons why it is useful to pre-allocate your arrays before the loop: it makes sure you have no old variables interfering with your loop. Better yet is to work with functions instead of scripts, but during the earliest stages that can be too much of a hassle.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

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

Gefragt:

am 27 Okt. 2017

Kommentiert:

Rik
am 27 Okt. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by