Make an empty array stay the same size
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jacob Partain
el 4 de Jul. de 2018
I have an array (c4) that I can't figure out how to keep the dimensions from changing.
% compares numbers to find when there is a sign change
signFind = find(fSlope(1:end-1)>0 & fSlope(2:end)<0 ...
| fSlope(1:end-1)<0 & fSlope(2:end)>0);
%creates an empty array called c4
c4 = int16.empty([1000,1,0]);
% inserts 'Inflection Point' in array c4 in the place indexed by the find function
for i = 1:size(signFind)
c4{signFind(i)} = 'Inflection Point';
end
But when c4 is modified in the indexing section, the length changes to the length of the last indexed number.
For example, if the last cell to have a sign change was cell 989, the the array will only go to that length but I need the array to stay at 1000 cells long.
Any help is very much appreciated.
5 comentarios
Guillaume
el 5 de Jul. de 2018
Editada: Guillaume
el 5 de Jul. de 2018
1:size(xxx) is always a mistake or very sloppy code writing. Again, size always return a vector of size at least 2. So your 1:size(xxx) is exactly the same as 1:[M N ...] Do you know the rule that governs the behaviour of the colon operator when passed an array as bound? I'd wager you don't.
As I said, your code only works by accident. If signFind had been returned as a row vector (1xN) instead of a column vector (Nx1) your code would not have worked at all, it would have been equivalent to 1:1. The correct syntax for a vector is
1:numel(signFind)
Respuesta aceptada
Guillaume
el 4 de Jul. de 2018
Editada: Guillaume
el 4 de Jul. de 2018
It's really unclear what you're trying to do. Note that an empty array cannot contain anything, that's the definition of empty! Therefore as soon as you put something in an array, it's no longer empty and the size must change.
As pointed out in the comments, you seem to be confusing matrices and cell arrays. You create your c4 as a (empty) array of int16 but then discard that and make it a cell array in the first step of your loop.
Perhaps you wanted to create a cell array whose cells are empty. In which case:
c4 = cell(1000, 1); %create a 1000x1 cell array with empty cells.
Más respuestas (1)
dpb
el 4 de Jul. de 2018
As Walter says, you allocated an int16 array but then trashed it by writing a cellstr to it; that change in type essentially created a new c4 as you overwrite the existing array with a noncompatible type. ML, being type-gnostic, just does what you asked it to do silently. If it's really just the string you want at the locations,
c4=cellstr(length(signFind),1); % cellstr array of length containing blank strings
signFind = (fSlope(1:end-1)>0 & fSlope(2:end)<0) | (fSlope(1:end-1)<0 & fSlope(2:end)>0));
c4(signFind) = 'Inflection Point';
1 comentario
Walter Roberson
el 4 de Jul. de 2018
Probably
c4(signFind) = {'Inflection Point'};
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!