Index of first empty cell in array?

12 Ansichten (letzte 30 Tage)
Frank
Frank am 18 Dez. 2014
Hi, I would like to find the index of the first empty cell in an array consisting of n rows and 4 columns. I need the index, because I would then like to add a certain value to each of the non-empty rows in column 3. I tried the following, but got an error message "Index exceeds matrix dimensions.":
n=1;
while ~isempty(array{n,3})
array_new{n,3}=array{n,3}+deltax;
n=n+1;
end
My idea is to find out the index of the first empty cell to feed my while-loop with this index. Thank you!

Antworten (2)

Giorgos Papakonstantinou
Giorgos Papakonstantinou am 18 Dez. 2014
If you want to add a certain value to each of the none empty rows in column 3 maybe you try the following:
array(cellfun(@(x) ~isempty(x), array(:,1)), 3)

Frank
Frank am 18 Dez. 2014
Bearbeitet: Frank am 18 Dez. 2014
Thank you for the answer. Does the 'x' represent the value I want to add to the array? If yes, I can't seem to get it working. Maybe I have to use the array for the ~isempty condition?
And do you by any chance know where my mistake is in my original code? In my example, row 1056 is the first empty row. However, Matlab doesn't stop counting at 1055 and once n reaches 1056, it produces the above message.
  1 Kommentar
Giorgos Papakonstantinou
Giorgos Papakonstantinou am 18 Dez. 2014
Bearbeitet: Giorgos Papakonstantinou am 18 Dez. 2014
The x is the argument of the function handle ~isempty(x). Read about the function handle here.
When the ~isempty(x) function is called, MATLAB assigns the value you pass in to variable x. In your case every cell entry of your array. (it does not correspond the value that you want to add).
A somewhat simplistic assumption of your array could be:
array =
[2] [3] [5] [3]
[] [1] [4] [1]
[4] [4] [5] [3]
[4] [4] [4] [1]
If you want i.e. to add 5 at each of the non empty rows of column 3, try this:
cellfun(@(x) x+5, array(cellfun(@(x) ~isempty(x), array(:,1)), 3), 'uni', 0).
This sentence will find the non-empty rows of the first column of array and it will add the value of 5 to the 3rd column. In the example that I gave you, it will be:
[7]
[7]
[6]

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by