Azzera filtri
Azzera filtri

How to take a number inside cell array

4 visualizzazioni (ultimi 30 giorni)
Hi,
For example I have a 3x2 cell matrix like this.
A {[32,28,30,31] [27,29,30] [32,29,31,27,28]}
B{[30,64,72,85] [15,33,62] [45,62,77,84,90]};
where every element has different size.
I want to take number in every element at B which corresponds to the max value of A in each element.
The expected results are to be like this C=[30],[62],[45]
How do I do this ? Thank you.

Risposta accettata

Stephen23
Stephen23 il 5 Lug 2021
A = {[32,28,30,31],[27,29,30],[32,29,31,27,28]};
B = {[30,64,72,85],[15,33,62],[45,62,77,84,90]};
F = @(a,b)b(max(a)==a);
C = cellfun(F,A,B)
C = 1×3
30 62 45
  3 Commenti
Ahmad Bayhaqi
Ahmad Bayhaqi il 6 Lug 2021
Modificato: Ahmad Bayhaqi il 6 Lug 2021
Just one more question, please.
What if the condition is with the nested cell array like this
A = {[27,28,30,31]},{[26,25,30]},{[33,29,31,27,28]};
B = {[30,64,72,85]},{[15,33,62]},{[45,62,77,84,90]};
How can I take a max value of all entry nested in A and pick a value in B which corresponds to the max value of A.
so the result will be:
C = 33
D = 45
Thank you
Stephen23
Stephen23 il 6 Lug 2021
A = {[27,28,30,31],[26,25,30],[33,29,31,27,28]};
B = {[30,64,72,85],[15,33,62],[45,62,77,84,90]};
C = [A{:}];
D = [B{:}];
[C,X] = max(C);
C
C = 33
D = D(X)
D = 45

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Data Types in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by