Count how many element are inside each cell af a cell array on the basis of an array

Hi given a cell array
V={{[1,1,1,1;25,45,70,90],[2,2,2,2;78,112,146,180],[3,3,3,3;93,127,161,195],[4,4;70,100],[6;85],[9,9;85,110]},{[],[2,2,2,2;73,107,141,175],[3,3,3,3;83,117,151,185],[4,4,4,4;65,85,105,125],[6;85],[9,9,9;80,105,130]}};
and an array
SP= [1 2 3 4 6 9]
I want to create a matrix M that has as many rows as the cell in V (in this case 2)
CONSIDERING THE FIRST CELL:
Inside the row of M how want to count how many elements are present in each cell of the same type, and that cannot exceed a value of 80 (<80) referring to the second raw of each cell.
considering element 1 in V{1,1} that occupies V{1,1}{1,1}
In this case the number of 1 that do not exceed 80 is 3.
take the element 2
is just 1.
Doing the same for all the elements and cells i ontain M:
M= [3 1 0 1 0 0; 0 1 0 1 0 1]
May someone help me with the code?

6 Comments

Is there a particular need to have a 1x2 cell array with two 1x6 cell arrays nested inside it? Why not just have one 2x6 cell array without any nesting (which would simplify the code)?
You do not explain what role SP has in your algorithm: perhaps it corresponds to your "considering element 1.." and "take the element 2...", but as none of the cell arrays have 9 elements, there is no way to "consider element 9" if it does not exist. You need to explain SP better.
SP as well as V are fixed and obtained from a previous code.
Do you wanna see the previous code? But I don’t know if this can help us to find a solution.
In any case, in a previous question that I made I have received an advice with something similar! That is :
VM = cellfun(@(z)cellfun(@(x)sum(x(2,:)<= 80),z),V,'UniformOutput',false);
In this case I don’t obtain a matrix but a cell. The problem is that a I don’t know why but it gives me error
If you wanna better understand SP. In my code I insert each time different values inside SP. the values of SP are attributes. So I can choose in a list from 1 to 9 ( if 9 is the last) and fill SP with just some of them but not all. In the previous case I decide to use 1 2 3 4 6 9.
I need to mantain this attribute fixed! In any case SP AND V are fixed input for the problem above
"SP as well as V are fixed and obtained from a previous code."
Sure, that is nice.
"Do you wanna see the previous code?"
I have no idea what "wanna" means, but it sounds rather painful, so I will decline your offer.
But I do want to know what role SP plays in your algorithm, which so far you have not explained. For example, in your question SP occurs just once (to define it and then never refer to it again). In your comments you explain that SP contains attributes... which is lovely, but it is not what I asked about. Perhaps SP bears no relevance, which so far appears to be the case.
Here is my question again, in slightly different words:
How do you use the values of SP in the algorithm that you ask about in your question?
The code in this comment will fail because that empty array does not have a second row.
SP is the input of my algorithm. I want to show you just a part of the code through which I have obtained V.
clear all;
clc
SP= [1 2 3 4 6 9];
M = [0 1 1 1 0 1 1 1 1];
T= [20 34 34 20 34 20 25 34 25];
GGG = {[1 2 2 1 3 4 9 9 6 1 3 3 2 1 2 4 3 ; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85],[ 2 2 3 3 4 9 4 9 6 4 9 3 3 2 2 4 ; 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 78]};
for i=1:numel(GGG)
G=GGG{i};
SP=unique(G(1,:));
for j=1:length(SP)
[~,indx]=find(G(1,:)==SP(j));
R{1,i}{1,SP(j)}=[SP(j); M(SP(j))*T(SP(j))];
V{1,i}{1,SP(j)}=[SP(j); M(SP(j))*T(SP(j))+G(2,indx(1))+T(SP(j))];
if length(indx)>=2
for k=2:length(indx)
R{1,i}{1,SP(j)}=[R{1,i}{1,SP(j)}, [SP(j); G(2,indx(k))-V{1,i}{1,SP(j)}(2,(k-1))]];
if R{1,i}{1,SP(j)}(2,end)<0
V{1,i}{1,SP(j)}=[V{1,i}{1,SP(j)}, [SP(j); G(2,indx(k))+T(SP(j))-R{1,i}{1,SP(j)}(2,end)]];
else
V{1,i}{1,SP(j)}=[V{1,i}{1,SP(j)}, [SP(j); G(2,indx(k))+T(SP(j))]];
end
end
end
end
end
SP= [1 2 3 4 6 9];
V = cellfun(@(x) x(:,SP),V,'uni',0);
As you can see I need SP every time in order to track which attributes I'm creating through the sequence. That is, 9 for example will always be in the 6th column. At the next run I want to substitute and use a new SP, for example [1 3 4 5 7 9]. The idea is that I will change every time and run the code with the values in SP, according to which attributes I want to produce.
Now, if the problem is
VM = cellfun(@(z)cellfun(@(x)sum(x(2,:)<= 80),z),V,'UniformOutput',false);
and I cannot found the second raw in the second cell. If we are able to insert just zeros in that cell that is empty, probably we solve the problem. So maybe in the code I post to you, is it possible to modify and obtain zeros? maybe modifying
V = cellfun(@(x) x(:,SP),V,'uni',0);
Or is there a smarter solution?

Sign in to comment.

 Accepted Answer

>> C = vertcat(V{:});
>> X = ~cellfun('isempty',C);
>> F = @(x)sum(x(2,:)<=80);
>> M = zeros(size(C));
>> M(X) = cellfun(F,C(X))
M =
3 1 0 1 0 0
0 1 0 1 0 1
Nothing in your question or comments seems to require SP, so I have ignored it.

1 Comment

You were right Stephen. SP was not helpful.
Thanks a lot and sorry for typo

Sign in to comment.

More Answers (0)

Products

Release

R2019b

Tags

Asked:

on 15 Oct 2019

Commented:

on 16 Oct 2019

Community Treasure Hunt

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

Start Hunting!