Ignore empty cell array and execute the following cell - arrays

I want to make a condition that check if the cell array is empty then move to the next.
I got this index exceed matrix dimensions
Looking forward to hear from you

You can use isempty to check if an element is empty or not:
C = cell(5,1);
C{2} = 2;
for ind = 1:length(C)
if ~isempty(C{ind})
disp(['Processing C{' num2str(ind) '}']);
end
end
and never let index of C exceed length(C).

Related

MATLAB: deleting elements from multi-level cell

I've got a large multi-level cell and I'm trying to delete parts of it within a for loop but can't get past "Error: The expression to the left of the equals sign is not a valid target for an assignment."
Have tried a few versions and keep hitting the same error.
for i=1:length(List{1}{17}{imagenum})
if List{1}{17}{imagenum}(i)>1
List{1}{17}{imagenum}(i)=[];
end
end
Separate question: When I delete parts of a cell/anything within a for loop, is there a standard/smart way of making sure the for loop stays on track? I've been using a variable to keep track of deletions but I'm assuming there's a smarter way of doing this:
deletions=0;
for i=1:length(List{1}{17}{imagenum})
if List{1}{17}{imagenum}(i-deletions)>1
List{1}{17}{imagenum}(i-deletions)=[];
deletions=deletions+1;
end
end
Let's assign List{1}{17}{imagenum} to a:
a = List{1}{17}{imagenum};
Is 'a' cell or array? If it is array:
for i = numel(a):-1:1
if a(i)>1, a(i) = []; end
end
Easier way should be:
a(a>1) = [];
If it is cell:
for i = numel(a):-1:1
if a{i}>1, a(i) = []; end
end
General rule: when you delete items in a loop, you start from the end, not the beginning.

How do I reassign even and odd indices of a character array into a new smaller character array in Matlab?

In matlab I have a 32x1 character array A such that
A = {'F1' 'F2' 'F3' 'F4' 'F5' 'F6' ... 'F32'};
A = A';
Now I am trying to do the following with A.
For every even index of A meaning
A{2}, A{4}, A{6}...
I want to assign those values to a 16x1 character array B and for the odd indices of A I want to assign those values to a different 16x1 array C.
I use the following code:
for i=1:32
if mod(i,2)==0
B{i} = A{i};
else
C{i} = A{i};
end
end
and it works, but only partially because it assigns the right values at for e.g. B{2} and B{4} but the values in B{1} and B{3} are the same as in B{2} and B{4}.
Can anybody tell me how to reassign even and odd indices of a character array into a new smaller character array? My problem is that I am going from a 32x1 into a 16x1 and I'm not sure how to avoid the extra 16 entries.
Many thanks!
To get this question actual answered, use the idea of Luis Mendo in the comments. You can combine it with deal to save one line of code:
[B, C] = deal(A(2:2:end), A(1:2:end))
To make your loop work, you need a second running index jj:
A = {'F1' 'F2' 'F3' 'F4' 'F5' 'F6'};
for ii = 1:6
jj = ceil(ii/2);
if mod(ii,2)==0
B{jj} = A{ii};
else
C{jj} = A{ii};
end
end

Pulling out specific column when sometimes, the array is empty

I am trying to pull out a column from inside a cell. However, sometimes, the cell is empty.
For example, if in this line, I try to pull out the last column inside PM25_win{i}, it sometimes has an array inside of size nx28. However, sometimes, the array is zero.
for i = 1:length(years)-1
PM25 = table2array(PM25_win{i}(:,end));
end
When the array is empty, the code stops and I get the error
Subscript indices must either be real positive integers or logicals.
How can I account for both cases so that the code will simply create the PM25 variable as an empty array if PM25_win{i} is empty?
You could simply add an if-else statement in the for loop.
for i = 1:length(years)-1
if isempty(PM25_win{i}(:,end))
PM25 = [];
else
PM25 = table2array(PM25_win{i}(:,end));
end
end

Matlab replacing 0 with empty cell in Cell array

I have a 10,000 x 65 Cell with 0's and 1's so for example if I type
C(1,1) I get [0] returned or likewise C(3,4) a [1] is returned
I need a way to turn every 0 into a blank cell and every 1 into a char t
I've tried the following with little success
[rows, cols] = size(M);
for i = 1:rows
for j = 1:cols
if strcmp(M(i,j), 1)
M(i,j) = 't';
end
end
end
It returns the same thing, I'm guessing its not recognising the 1's as strings. Any idea sort of simply doing the conversion straight in Excel.
thanks
You are not accessing the cell data-structure correctly.
First of all, if M really is a cell array, you will have to use M{i,j} to access the data.
What M(i,j)does is just create a sub-cell-array, which contains M{i,j} as entry.
Also strcmp isn't used correctly, if your cell array contains strings you should use strcmp(M{i,j}, '1').
If your cell array on the other hand contains integers, you would have to use: M{i,j}==1.

Is there a more efficient way of choosing an element from 1 array and to every element of the same array

I want to, for every element of an array ZAbs, compare it for equality to every element of the array itself and put them into another distinct array. I want the distinct array's elements to have the same index as the ZAbs array.
I did this by creating 4 nested for loops:
for pAbs2 = 1:400
for qAbs2 = 1:300
zAbsCompare = ZAbs(qAbs2, pAbs2);
for pAbs3 = 1:400
for qAbs3 = 1:300
zAbsCompare2 = ZAbs(qAbs3, pAbs3);
if (zAbsCompare == zAbsCompare2)
InitialZModEqualsImag(pAbs2,qAbs2) = InitialZImag(qAbs2, pAbs2);
InitialZModEqualsReal(pAbs2,qAbs2) = InitialZReal(qAbs2, pAbs2);
end
end
end
end
end
However, this runs really slowly. I can't think of a better way to do this, but as I'm inexperienced with MATLAB there's probably something I'm overlooking here. Any help?
EDIT: Fixed an error and restated the question.
You can do the comparison (not sure that's what you want) efficently with bsxfun:
comp = bsxfun(#eq, X, shiftdim(X,-2));
The result comp(m,n,p,q) is 1 if X(m,n) == X(p,q), and 0 otherwise.

Resources