필터 지우기
필터 지우기

Apply a function to a specific column of multiple matrices?

조회 수: 1 (최근 30일)
Hi Guys. I would very like to have help on this. I have 4 sensors that give me 4 double matrices. I'd like to apply a function to convert mm into m, but I would like to create it for each column at a time. I know how to do that for both columns of each matrix, but I like to do that separetely just to have different outputs for each axis in the matrix.
For instance: I have 4 matrices called Sen1 to Sen4. They are double matrices 10000x2. I'm trying this:
for i=1:4
eval(['XSen' num2str(i) '=Sen' num2str(i) './1000;'])
end
But I would like to apply it only for the first column, and after to the second, changing XSen to Zsen. Any thought on how to solve it.
Thanks.

채택된 답변

Clayton Gotberg
Clayton Gotberg 2021년 4월 17일
편집: Clayton Gotberg 2021년 4월 17일
When you use element-wise multiplication with a matrix and a vector, MATLAB makes some assumptions to make the code simpler. For example,
A = [1 1; 2 2; 3 3];
B = [10 1];
A.*B
% What MATLAB actually does:
[1 1; 2 2; 3 3].*[10 1; 10 1; 10 1]
% Result:
A.*B = [10 1; 20 2; 30 3];
So, you can create XSen = Sen.*[1/1000 1].
Instead of creating a series of variables with eval, just write the four lines in this way. It will be just as easy and you will avoid the danger of using eval as well as making your code far more understandable.
  댓글 수: 3
Clayton Gotberg
Clayton Gotberg 2021년 4월 17일
No one will tell you to use eval in this application. It is a terrible misuse of that tool.
If you must use it here, you need to add the indexing for the column that you want to apply the transformation to.
eval(['XSen' num2str(i) '=Sen' num2str(i) '(:,2)./1000;']) %for example
Seriously, though, don't do it this way. It's worse in every way that I can think of and the only benefit is that you don't have to name individual variables. Instead, just use a cell array or a structure.
Thiago de Aquino Costa Sousa
Thiago de Aquino Costa Sousa 2021년 4월 17일
Thank you Clayton. I will have your advises in mind. It worked properly.

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

추가 답변 (0개)

카테고리

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