필터 지우기
필터 지우기

How to apply function on columns while skipping certain columns

조회 수: 3 (최근 30일)
Zuha Yousuf
Zuha Yousuf 2020년 3월 19일
댓글: Zuha Yousuf 2020년 3월 19일
So I have a matrix that has 31413 rows and 950 columns. I want to apply a function starting from column 2 till column 10, skipping column 11, then applying the same function from coumn 12 till column 20, then skipping column 21 and applying the function on column 22 till column 30 and so on.
Basically something like this
[col1 col2...col10 col11 col12...col20 col21 col22...col30 and so on until column 950]
(the columns in between are where I want to apply the function basically)
Is there any way I can do this? Any help appreciated!!!

채택된 답변

Sriram Tadavarty
Sriram Tadavarty 2020년 3월 19일
Hi Zuha,
You can use a for loop as below and it does what is required, provided you expect the same amount of size in rows and columns from the anonymous function:
m = rand(31413,950);
for i = 2:10:size(m,2)
m(:,i:i+8) = anonymousFun(m(:,i:i+8)); % anonymousFun could be tril, provided you wanted to get the same size of matrix
end
Hope this helps.
Regards,
Sriram

추가 답변 (1개)

Mohammad Sami
Mohammad Sami 2020년 3월 19일
m = rand(31413,950);
allcols = 1:950;
exccols = 1:10:950;
applcols = allcols(~ismember(allcols,exccols));
out = max(m(:,applcols)); % some builtin functions can be used directly
% or
out = arrayfun(@(x)max(m(:,x)),applcols);
  댓글 수: 2
Zuha Yousuf
Zuha Yousuf 2020년 3월 19일
Hi! when I do as you say, it only gives me those columns I applied the function on as output. As my output, I also need the columns I skipped over when applying the function.
Like this:
[col1 appliedfunction(col2...col10) col11 appliedfunction(col12...col20) and so on until col950]
Zuha Yousuf
Zuha Yousuf 2020년 3월 19일
편집: Zuha Yousuf 2020년 3월 19일
And also, i want that the function be applied only from col2 till col 10, and then separately on col 12 till col 20 and so on. For example, you used max on all the columns aside from the ones I wanted excluded. I need max to be used individually again and again on these small subsets of the matrix. Is there a way to do that?

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

카테고리

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