Make an empty array stay the same size
Show older comments
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 Comments
Walter Roberson
on 4 Jul 2018
Why are you creating an int16 array but then overwriting the variable as a cell array?
Jacob Partain
on 4 Jul 2018
Edited: Jacob Partain
on 4 Jul 2018
Guillaume
on 4 Jul 2018
Also, why the square brackets around the find(...)?
Also 1:size(signFind) may work but only by accident. size(signFind) is a vector of 3 elements. The colon only use the first element. so it is equivalent to 1:size(signFind, 1).
Jacob Partain
on 4 Jul 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)
Accepted Answer
More Answers (1)
dpb
on 4 Jul 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 Comment
Walter Roberson
on 4 Jul 2018
Probably
c4(signFind) = {'Inflection Point'};
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!