Reputation: 79
I have the following variable:
QOS= [100 200 120 300 45 88 90 500 66 180];
I have MPR
that is a n x n
cell. It contains some cells that are 2 x 2
double and cells that are 1 x 2
double. I want for the cells that are 2 by 2 double (for example [2 3 ; 4 5 ]
) to calculate the QOS for each row (ex: QOS(2)+ QOS(3) = 200+120= 320
), then return the row that has the highest QOS value (ex: 4 5
in this example).
I want for the cells that are 1 by 2 double (for example [5 6]
) to calculate the QOS for each element (ex: QOS(5)
= 45), then return the element that has the highest QOS value (ex: 6
in this example).
How could I do such thing??
Upvotes: 0
Views: 71
Reputation: 104474
Because of the way your cell array is structured, the easiest way is to use a for
loop. I'm sure there are other ways, but for getting something working, let's use a for
loop.
For each of your cells in your cell array, you would use the matrices to access your QOS
variable then sum across the columns. Because your cell array has 1 x 2
matrices, summing over the columns won't give the right result. As such, in order for our logic to work, you'll need to make sure this is a column vector. I'll put that logic inside the for
loop in case.
Once you're done, you would then return the row indices of each cell that corresponds to the maximum sum. In other words, given your matrix MPR
, and the variable out
will store the output, try doing this:
QOS = [100 200 120 300 45 88 90 500 66 180]; %// From your post
out = cell(1,numel(MPR)); %// Create output cell array
for idx = 1 : numel(MPR) %// For each cell in MPR...
matr = QOS(MPR{idx}); %// Access QOS variables defined by MPR cell
if size(matr,1) == 1 %// Make sure it's a column vector if row vector (1 x 2)
matr = matr(:);
end
[~,loc] = max(sum(matr,2)); %// Sum across all the columns
out{idx} = matr(loc,:); %// Output row of MPR cell that has max sum
end
As an example, supposing MPR
was your example you have above:
MPR{1} = [2 3; 4 5];
MPR{2} = [5 6];
Running the above code, we get:
>> celldisp(out)
out{1} =
4 5
out{2} =
6
Upvotes: 1