필터 지우기
필터 지우기

How to define several variables from vector at the same time

조회 수: 169 (최근 30일)
Martin Olafsen
Martin Olafsen 2017년 10월 11일
편집: Bill Tubbs 2020년 2월 3일
Hi
I am trying to define several variables at the same time. The data for each variable is saved in a vector. I have tried the following:
x = [1 2 3];
y = [4 7 9];
[a b] = [x(end) y(end)]
%Or alternatively
[c d e] = [x(1) x(2) x(3)]
When I try this I receive the "Too many output arguments." error. Is there some easy way to do this?
I realize I could use:
a = x(end)
b = y(end)
But I would like a cleaner method.
Regards

채택된 답변

Jan
Jan 2017년 10월 11일
편집: Jan 2017년 10월 11일
[a, b, c] = deal(x(1), x(2), x(3))
I am not sure that this is "cleaner", if you do this for many variables. It is slightly slower than the direct and simple:
a = x(1);
b = x(2);
c = x(3);
deal and num2cell have a certain overhead. The code might be more compact, if you do this in one line instead of 3, but internally much more code lines are processed. I'm convinced, that the code will not suffer from additional lines, if they are such simple and clear: neither a human reader nor the Matlab interpreter will have any problems with them. Do not try to optimize the optical appearance of the code. It is more valuable to keep the code fast (to optimize the run time) and simple (to optimize the time for debugging and maintaining).
  댓글 수: 3
Jan
Jan 2017년 10월 12일
편집: Jan 2017년 10월 12일
@Martin: This is a completely different question. The solution is easy:
function [xf, yf] = Cycle(fluid, massflow)
... calculate x and y
xf = x(end);
yf = y(end);
end
as you have mentioned in your question already. This is clean, clear and fast. Searching for more complicated methods like deal() is a waste of time - for programming and for the processing.
Remember Donald Knuth's mantra:
KISS - Keep it simple stupid
Perhaps the bigger problem is that you have 17 vectors in your code. Would a matrix or a cell array be smarter? Then the extraction of the last element could be done in a loop or by a simple indexing. Returning a vector instead of a bunch of variables might be an option also.
Bill Tubbs
Bill Tubbs 2020년 2월 3일
편집: Bill Tubbs 2020년 2월 3일
From the title, I thought this would answer my problem but it doesn't. I have a vector params and I want to extract three parameters from it.
This doesn't work:
[b1, a1, a2] = deal(params(2:4));
deal(X) copies the single input to all the requested outputs.
But why the need for any function to do this? Wouldn't it be nice and logical if this worked:
>> [b1, a1, a2] = params(2:4);
Insufficient number of outputs from right hand side of equal sign to satisfy assignment.
I see there is a note in the documentation saying:
Beginning with MATLAB® Version 7.0 software, you can access the contents of cell arrays and structure fields without using the deal function. See Example 3, below.
Why not allow it for vectors as well?

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

추가 답변 (2개)

KL
KL 2017년 10월 11일
편집: KL 2017년 10월 11일
You can use cell arrays and get the comma separated lists,
xy = num2cell([x y])
[a b] = xy{1,[3 6]};
[c d e] = xy{1, 1:3};

Walter Roberson
Walter Roberson 2017년 10월 11일
[c, d, e] = deal(x(1), x(2), x(3));

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by