subtracting a constant from all the array elements in a structure

조회 수: 1 (최근 30일)
AP
AP 2011년 6월 16일
I have defined an array of struct, named A. A is the profile by which the experiment is done. There are 4 profiles.
A has a member named B which is a cell array. B is of size N×1 where N is the number of experiments, approximately 5000. Elements of B are M×1 vectors. M varies with respect to the experiment.
For example, A(3).B{2} is for profile#3 and experiment#2.
I am trying update the values of B for each profile by subtracting 4 different constants from the elements of B. I tried the following but I get error.
cons(1)=1;
cons(2)=2;
cons(3)=3;
cons(4)=4;
for i=1:4
A(i).B=arrayfun(@(x) (x-cons(i)),[A(i).B)]);
end
I would appreciate your help. :)
  댓글 수: 2
AP
AP 2011년 6월 16일
I figured it out by using:
A(i).B=cellfun(@(x) (x-cons(i)), A(i).B, 'uniformOutput',false);
AP
AP 2011년 6월 16일
I would be thankful for suggesting a better solution, :)))))

댓글을 달려면 로그인하십시오.

답변 (1개)

Andrei Bobrov
Andrei Bobrov 2011년 6월 16일
V=squeeze(struct2cell(A));
V2 = arrayfun(@(x)cellfun(@(y)y-cons(x),V{x},'un',0) ,1:length(A),'un',0);
A = struct('B',V2)
it variant without loop, but I think your variant better

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by