Create empty rows in a cell, based in a condition
Show older comments
Hello everyone,
I have a cell (390x1) and it should repeat the words 'hello, 'hey', 'hi' and 'hoi' every 4 rows, as in the following:
Row 1 - 'hello'; Row 2 - 'hey'; Row 3 - 'hi'; Row 4 - 'hoi'; Row 5 - 'hello'; Row 6 - 'hey'; Row 7 - 'hi'; Row 8 - 'hoi'.
However, sometimes it goes like:
Row 9 - 'hello'
Row 10 - 'hey'
Row 11 - 'hoi'
And it skips the 'hi' row, making it so that I can't loop it using something like "for i = 1:4:390", which is what I need.
What I want to do is create the same cell where the "3rd row" of every "4 rows" is empty, that is: when the 3rd row has "hi" in it, those 4 rows stay the same. But when the "hi" row is missing, it creates an empty cell row between the 2nd and the 4th row (between the 'hey' and the 'hoi').
For example, it would be come:
Row 9 - 'hello'
Row 10 - 'hey'
Row 11 - ' ' (empty)
Row 12 'hoi'
I already tried doing this with loops and the strcmp function, but I really can't do it.
Please ask questions in case I didn't explain myself well enough.
Thank you
Accepted Answer
More Answers (1)
Constantino Carlos Reyes-Aldasoro
on 3 Feb 2020
Hello
I do not know why there should be rows that are skipped, unless you are copying and pasting and something goes wrong. I have been able to do what you wanted like this:
>> for k=1:4:390
a{k,1}='hello';
a{k+1,1}='hey';
a{k+2,1}='hi';
a{k+3,1}='hoi';
end
Notice that to have the answer as rows you have to specify the column {k,1} otherwise the default will fill a value per column. Now if you want to have a blank space you can do it like this:
>> for k=1:4:390
b{k,1}='hello';
b{k+1,1}='hey';
b{k+2,1}='';
b{k+3,1}='hoi';
end
I hope that this answers your question, if it does not, let me know. If it does, please accept the answer.
4 Comments
Happy Bear
on 3 Feb 2020
Constantino Carlos Reyes-Aldasoro
on 3 Feb 2020
Ok, so you already have the cell, and in some cases the info is, let's say "corrupted" and what you want is to "clean" it by introducing a position corresponding to the third in a group of four. So, going back to my example
>> for k=1:4:390
a{k,1}='hello';
a{k+1,1}='hey';
a{k+2,1}='hi';
a{k+3,1}='hoi';
end
That would be the correct set, and this will skip one "hi" in position 23
>> for k=1:22
b{k,1}=a{k,1};
end
>> for k=23:390
b{k,1}=a{k+1,1};
end
So to solve the problem you need to find if 4 consecutive locations have the correct sequence, you can test it like this
>> k = 1;
>> strfind(b{k},'hello')&strfind(b{k+1},'hey')&strfind(b{k+2},'hi')&(strfind(b{k+3},'hoi'))
ans =
logical
1
>>
Now you have to loop again in groups of 4
for k=1:4:390
if strfind(b{k},'hello')&strfind(b{k+1},'hey')&strfind(b{k+2},'hi')&(strfind(b{k+3},'hoi'))
% It complies, copy to a new cell, say d
d{k}=b{k};d{k+1}=b{k+1};d{k+2}=b{k+2};d{k+3}=b{k+3};
else
% it does not comply, the hi is missing
d{k}=b{k};d{k+1}=b{k+1};
d{k+3}=b{k+2};
%introduce the hi
d{k+2}='hi';
end
so the first part of the if is when the four cases are present, you copy, in the second you introduce. The only thing to take into account is that if the hi is missing you might have to alter the k so that it goes one before.
That should work, hopefully!
I would strongly recommend using strcmp instead of strfind. The above will completely fail if for example the sequence was {'ho', 'hoi'} instead of {'hi', 'hoi'}.
I would also recommend:
- to preallocate d instead of growing (slowly) in the loop
- using numel(b) for the end bound instead of an hardcoded bound which would need changing if b length changes
- Using indices range for copying instead of individual copies (i.e. d{k:k+3} = b{k:k+3})
The above will also fail if more than 4 'hi' are missing.
edit: actually, the above will fail after the first missing 'hi' since from then one, the 'hello' is no longer on a multiple of 4 index.
Happy Bear
on 4 Feb 2020
Categories
Find more on Loops and Conditional Statements 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!