How to make it a one line code?

조회 수: 18 (최근 30일)
oshawcole
oshawcole 2017년 12월 6일
편집: Walter Roberson 2023년 10월 15일
matrix= [1 0 -1 0.001 4; 5.9 -1 3.15 1 1.11 ] ;
cell_conv = (num2cell(matrix));
find_one = abs( matrix ) == 1 ;
cell_conv(find_one)={'hey'}
  댓글 수: 2
KSSV
KSSV 2017년 12월 6일
You can striagh away use:
cell_conv(abs( matrix ) == 1)={'hey'}
oshawcole
oshawcole 2017년 12월 6일
편집: Walter Roberson 2023년 10월 15일
cell_conv = (num2cell(matrix));
find_one = abs( matrix ) == 1 ;
cell_conv(find_one)={'hey'}
Merging these three to one line. how do I put num2cell in the single line code you gave?

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

채택된 답변

Walter Roberson
Walter Roberson 2017년 12월 6일
It is possible to do in one line, but it gets so messy that it is not recommended.
Here is a way to do it with the assistance of a helper function.
IDXAT = @(M,IDX) M(IDX);
disp(IDXAT(cat(3,num2cell(matrix),repmat({'hey'},size(matrix))), reshape(1:numel(matrix), size(matrix))+numel(matrix)*(abs(matrix)==1)))
The output would look like
'hey' [ 0] 'hey' [0.001] [ 4]
[5.9] 'hey' [3.15] 'hey' [1.11]
I am not sure that is acceptable to you.
  댓글 수: 2
oshawcole
oshawcole 2017년 12월 6일
yes you are right, it is messy I will try asking it with less constraints.
Walter Roberson
Walter Roberson 2017년 12월 6일
The above is the cleaner version -- doing it with one line would have required using the internal function subsref(), which I do not recommend that anyone deliberately use unless they are implementing object classes.

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

추가 답변 (1개)

Jos (10584)
Jos (10584) 2017년 12월 6일
Here is another but shorter two-liner using a functional programming approach for anonymous function:
matrix= [1 0 -1 0.001 4; 5.9 -1 3.15 1 1.11 ]
iif = @(varargin) varargin{2 * find([varargin{1:2:end}], 1, 'first')}() ;
% functional programming of a "if ... elseif ... else" construction
out = arrayfun(@(X) iif(isequal(abs(X),1), @() 'hey', true, @() X), matrix, 'un', 0)
  댓글 수: 3
Jos (10584)
Jos (10584) 2017년 12월 7일
Credits should go to Loren Shure
Walter Roberson
Walter Roberson 2017년 12월 8일
Ah, I remember now why I had forgotten Loren's solution: it was because I cannot think of any way of tricking MATLAB to return something for commands that return no output. For example if one of the branches was
@(x) set(handles.push7, 'Value', x)
and the other branch was
@(x) fprintf('hey')
then the fprintf() officially returns the number of bytes emitted so it can be used in arrayfun, but the set() has no output and cannot be used, not even when 'uniform', 0 is used.
I suppose I could write a real function
function yelm = empty(function_handle);
function_handle();
yelm = [];
end
and then code with
@(x) empty( @() set(handles.push7, 'Value', x) )
But I dislike having to write true functions to implement functional programming.
I know in HG2 that I could write
handles.push7.Value = x
and that returns handles.push7, but assignment in functional programming is ugly in MATLAB... though you can get this particular one to work:
newval = arrayfun(@(x) subsasgn(handles.push7, struct('type','.','subs','Value'), x),randi(7,1,5))
The output variable is mandatory in this situation.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by