Reputation: 709
I have a cell with dimension 41X41 as shown below
that has values equal to 1, it means all its values are equal to ones as shown below:
Based on many values, I could not include it here in one figure, but its dimension is 41 x 41.
What I was trying to do is calculate the number of ones in each row minus one as shown in the snippet of line code below:
ccc = sum(isSmaller{cc,:} == 1)-1
In order to get a cell with 1 row with 40 columns dimension that has 40s. as shown below:
My problem is instead of 40 columns is still showing 41 columns after deleting one from each row as indicated below.
May I get assistance, please? I need it to be 1 row with 40 columns.
Note: I do not care for the first 40 rows or columns or the lasts or the middle ones. I just need it to be done by itself only by minus one. After that, I need to sum all the 40s in this ccc = 1x40.
Below is my try:
for cc = 1:length(isSmaller)
ccc = sum(isSmaller{cc,:} == 1)-1
end
Upvotes: 0
Views: 97
Reputation: 18484
If I understand correctly, isSmaller
is a 1-by-1 cell array whose single cell contains a 41-by-41 matrix of logical values all equal to 1
(true
):
isSmaller={true(41)};
Then you wish to calculate the sum of each row of this matrix inside of a cell and subtract one from the result:
sum(isSmaller{1},1)-1
But since you only care about the first 40 rows you can trim off the last row with:
sum(isSmaller{1}(:,1:40),1)-1
Or:
sum(isSmaller{1}(:,1:end-1),1)-1
This returns a 1-by-40 matrix where each element is 40
.
Finally, you want to sum over the 40 elements of this matrix:
sum(sum(isSmaller{1}(:,1:end-1),1)-1)
This returns 1600
, as expected.
No for
loops are needed if this is all you need to do and your example is representative. I urge you to read through Matlab's documentation on cell arrays, in particular on how to access data within them.
Upvotes: 2