Make an empty array stay the same size

5 visualizaciones (últimos 30 días)
Jacob Partain
Jacob Partain el 4 de Jul. de 2018
Editada: Guillaume el 5 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
Jacob Partain
Jacob Partain el 4 de Jul. de 2018
The square brackets are there basically a visual reference for me but i will edit it out of the main question since it doesn't change anything.
The 1:size(signFind) is there to run through the indices found by signFind to place the string in the correct cell of the array c4. I tried it a few different ways before coming to that solution and I'm sure there is a better way, but it works.
The code in the main question works except that it overwrites the dimensions of c4 and I need c4 to keep the same dimensions and only write 'Inflection Point' into the cells indexed output by signFind variable.
Also, thank you both for the quick replies!
Guillaume
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)

Iniciar sesión para comentar.

Respuesta aceptada

Guillaume
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.
  1 comentario
Jacob Partain
Jacob Partain el 4 de Jul. de 2018
You're absolutely right. I understood the empty() function wrong. This was what I should have used to create the array.
Thank you so much.

Iniciar sesión para comentar.

Más respuestas (1)

dpb
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';

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by