Output shape inconsistent when indexing with empty vector

1 vue (au cours des 30 derniers jours)
Nathan Jessurun
Nathan Jessurun le 17 Juil 2019
Commenté : Nathan Jessurun le 7 Août 2019
I am working with code that maintains three lists -- two exist in the same matrix and one is separate:
twoLists = 1:9;
twoLists = [twoLists' 2*twoLists'];
singleList = (1:9)';
I then index into these lists as follows:
entries = [1;2];
first = twoLists(entries, 1);
second = twoLists(entries, 2);
xponding = singleList(entries);
This is all well and good until 'entries' is empty and both sets of lists only contain one entry:
twoLists = [1 1];
singleList = [2];
entries = ones(1, 0);
first = twoLists(entries, 1);
second = twoLists(entries, 2);
xponding = singleList(entries);
In this case, the shape of 'xponding' doesn't match 'first' and 'second'. I could fix the problem by indexing into singleList like:
singleList(entries, 1);
but I was wondering if there was a better way to solve the problem / elminate it entirely, since it would be a pain to index like this for the whole function.
  1 commentaire
madhan ravi
madhan ravi le 17 Juil 2019
show how your desired result should look like

Connectez-vous pour commenter.

Réponses (1)

Nikhil Sonavane
Nikhil Sonavane le 2 Août 2019
You are encountering the undesired output because of the following line in the code-
entries = ones(1, 0);
Indexing matrices in MATLAB should always be done using positive integers. I suggest you to always start indexing all the matrices from 1 and not zero as you tried it in your first example. In your second example you are creating a matrix with dimensions 1x0 and later assessing another matrix using the previous matrix which has undefined dimensions.
  1 commentaire
Nathan Jessurun
Nathan Jessurun le 7 Août 2019
I tried to simulate a situation that appears in my code. 'entries' is formed from a logical index of potential values. So if
values = 1:3
solutions = logical([0 0 0]);
entries = values(solutions);
I get the situation I described.
It doesn't change with how I initialize 'entries'. Does that make sense? Sorry, the code base is large and I attempted to minimize the complexity of the question.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by