1 view (last 30 days)

clc

clear all

a=1

b=0

while irrelevant

b = [a] % The answer I want from this statement is b = [1]

a = [a b] % The answer I want from this statement is [1 0] , I dont want to use calculated b (from the statement before) but b=0 ('the first b').

end

% I dont want to really want to make b0,b1,b2,b3 because this needs to become recursive with an 'while' and 'for' statement.

% So I really want to execute b = [a] and a = [a b] at the same time; but I cannot figure out how to fix this.

ADDON:

Thank you for your suggestion.

Although it doesn't really work for our problem.

We'd like the following to be recursive:

... etc ...

clc

clear all

a1=1

b1=0

b2=[a1]

a2=[a1 b1]

b3=[a2]

a3=[a2 b2]

b4=[a3]

a4=[a3 b3]

... etc ...

Walter Roberson
on 11 Oct 2020

NO.

You cannot do what you want in MATLAB.

At the moment, I cannot think of any programming language that supports what you are asking for.

Alan Stevens
on 11 Oct 2020

What about a temporary variable for b:

...

bt = b;

b = [a];

a = [a bt];

...

Steven Lord
on 12 Oct 2020

Use a cell array (or two cell arrays, though b is a copy of most of a.)

a = {1};

b = {0};

cellToFill = 2;

while cellToFill < 20

b{cellToFill} = a{cellToFill-1};

a{cellToFill} = [a{cellToFill-1}, b{cellToFill-1}];

cellToFill = cellToFill + 1;

end

Instead of referring to numbered variables, refer to cells in a and b.

a{4}

b{5}

Steven Lord
on 12 Oct 2020

So you need all permutations of 8 ones and 4 zeros? That's a very different question than the one you asked.

If that's not the full question please state exactly what you're trying to do. In particular, what restrictions are there (if any) about where the 1's can be placed.

Alan Stevens
on 12 Oct 2020

I'm obviously not understanding something here (not unusual!),. Doesn't the following do what you want:

a = 1;

b= 0;

n = 5;

[a, b] = recur(a,b,n);

disp('Recursive')

disp(a)

disp(b)

% Compare with

a1 = 1; b1 = 0;

b2 = a1; a2 = [a1 b1];

b3 = a2; a3 = [a2 b2];

b4 = a3; a4 = [a3 b3];

b5 = a4; a5 = [a4 b4];

b6 = a5; a6 = [a5 b5];

disp('Incremental')

disp(a6)

disp(b6)

function [a, b] = recur(a,b,n)

if n>1

bt = b;

b = a;

a = [a bt];

n = n-1;

[a, b] = recur(a,b,n);

else

bt = b;

b = a;

a = [a bt];

end

end

This produces the following comparison:

Recursive

1 0 1 1 0 1 0 1 1 0 1 1 0

1 0 1 1 0 1 0 1

Incremental

1 0 1 1 0 1 0 1 1 0 1 1 0

1 0 1 1 0 1 0 1

Bruno Luong
on 12 Oct 2020

Edited: Bruno Luong
on 12 Oct 2020

I'm surpised nobody proposes yet a very MATLABish solution

[a,b] = deal([a b],a]

Opportunities for recent engineering grads.

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

Start Hunting!
## 3 Comments

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/610616-i-want-to-make-a-recursive-formula-and-execute-two-statements-with-the-same-variables-at-the-same-ti#comment_1054581

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/610616-i-want-to-make-a-recursive-formula-and-execute-two-statements-with-the-same-variables-at-the-same-ti#comment_1054581

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/610616-i-want-to-make-a-recursive-formula-and-execute-two-statements-with-the-same-variables-at-the-same-ti#comment_1054666

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/610616-i-want-to-make-a-recursive-formula-and-execute-two-statements-with-the-same-variables-at-the-same-ti#comment_1054666

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/610616-i-want-to-make-a-recursive-formula-and-execute-two-statements-with-the-same-variables-at-the-same-ti#comment_1056736

⋮## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/610616-i-want-to-make-a-recursive-formula-and-execute-two-statements-with-the-same-variables-at-the-same-ti#comment_1056736

Sign in to comment.