简体   繁体   中英

Apply the same function multiple time with different sets of arguments

Here is part of my function, I want to past 4 sets of data into my function blood_type .

In each set, one of the arguments is numeric and another is character .

For the output Type and Vol , each of them is a 3D-matrix of size 80*80*2 .

在此处输入图片说明

I want to have a neat way such that I can obtain an ALL_TYPE which is a result by concatenating 4 outputs of blood_type (each type is from each patient_id and patient_name ).

ALL_TYPE = cat(3, Type1, Type2, Type3, Type4)

Similarly, I want to have

ALL_VOL = cat(3, Vol1, Vol2, Vol3, Vol4)

Instead of writing:

[Type1 Vol1] = blood_type(1, 'Ann');
[Type2 Vol2] = blood_type(2, 'Ben');
[Type3 Vol3] = blood_type(3, 'Chris');
[Type4 Vol4] = blood_type(4, 'David');

Are there any ways that can choose the pair of arguments and produce the outputs more efficient? It is because I have hundreds of patients and this will be cumbersome if I type hundreds of times the names and their IDs.

Thanks in advance.

Here's an approach, using cellfun ;

%'The arguments of the function need to be typed once anyways'
patient_id   = {1,2,3,4};
patient_name = {'Ann','Ben','Chris','David'};

[ALL_TYPE, ALL_VOL] = cellfun( ...
   @blood_type, patient_id, patient_name, ...
   'UniformOutput', false ...
);

ALL_TYPE = cat(3, ALL_TYPE{:});
ALL_VOL  = cat(3, ALL_VOL{:});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM