element wise logical operators?

조회 수: 23 (최근 30일)
Timj2004
Timj2004 2012년 8월 13일
I have a question regarding using logical operators on a multi-dimensional array.
I want to firstly test if an element passes a criteria then replace that value with another value given the results of a test.
I want to check the first dimension of the array against the threshold of 0.5 and replace all instances where this is true with a value for only 2 columns of the the 2nd dimension and all cases of the 3rd and 4th dimension. Does anyone know how to do this without multiple for loops?
for example
DA = rand(4,4,2,3);
if DA(:,2:4,:,:)<0.5;
DA(:,2:4,:,:) = 1;
end
thanks Tim

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2012년 8월 14일
편집: Azzi Abdelmalek 2012년 8월 14일
clear
DA = rand(4,4,2,3);
K=arrayfun(@(x) x<0.5,DA(:,2:4,:,:))
B=DA(:,2:4,:,:);
B(find(K==1))=1;
DA(:,2:4,:,:)=B
  댓글 수: 1
per isakson
per isakson 2012년 8월 14일
편집: per isakson 2012년 8월 14일
Explicitly operating on a sub array and assign it back is a good approach. However, may I propose a bit of refactoring according to Matt Fig above:
sub_array = DA(:,2:4,:,:);
sub_array( sub_array < 0.5 ) = 1;
DA(:,2:4,:,:) = sub_array;

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

추가 답변 (2개)

Matt Fig
Matt Fig 2012년 8월 13일
편집: Matt Fig 2012년 8월 14일
Do you mean this?
DA = rand(4,4,2,3);
DA(DA(:,2:4,:,:)<0.5) = 1;
or perhaps you are talking about this (see the comments below):
DA = rand(4,4,2,3);
tmp = DA(:,2:4,:,:);
tmp(tmp<.5) = 1;
DA(:,2:4,:,:) = tmp;
  댓글 수: 3
Matt Fig
Matt Fig 2012년 8월 13일
편집: Matt Fig 2012년 8월 13일
I wasn't sure whether the OP wants this or something more like:
DA = rand(4,4)
tmp = DA(:,2:4);
tmp(tmp<.5) = 1;
DA(:,2:4) = tmp
per isakson
per isakson 2012년 8월 14일
편집: per isakson 2012년 8월 14일
It turned out, I cannot make sense of the question. OP must help.
However, I found it very hard to grasp
M( logical_index ) = scalar;
when size(M) is not equal to size(logical_index). One dimension is ok, but four is not.
Is there a way to think about it?
OK, now I got the message: Work with sub arrays!

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


Timj2004
Timj2004 2012년 8월 14일
Ok, it appears there is some confussion regarding the question. I appologise. and I appreciate the responses. the idea is to review 3 columns of the array and compare the value to a limit. Then replace the value of the rows in those columns with a new value.
so if my 4D array is as follows DA(simulations, components, alpha, bravo) we have the simulation results (rows) for each component (columns) at each alpha and bravo.
so I want to run a check on 3 components for all simulations and replace the output of the simulation results if it meets a logical criteria.
So DA(sims, [component2 component3 component4] , A, B). lets say DA = rand(4,4,2,2) for now.
so DA(:,2:4,:,:)>0.5 = 1; so if sims in columns 2:4 are greater than 0.5, replace those results with 1.
Thanks.
I was trying to avoid creating too many variables here since I am dealing with arrays of the magnitude of DA(2000,80,50,8) but I guess the best solution has been to use sub arrays. thanks.
"sub_array = DA(:,2:4,:,:);
sub_array( sub_array < 0.5 ) = 1;
DA(:,2:4,:,:) = sub_array;" - per isakson
-sincerely Tim

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by