Home Unpacking a vector into k variables
Reply: 1

Unpacking a vector into k variables

cᴏʟᴅsᴘᴇᴇᴅ
1#
cᴏʟᴅsᴘᴇᴇᴅ Published in 2018-02-13 05:17:20Z

I'm learning matlab and trying to implement small angle approximation using vectorisation. This is the formula I'm trying to implement:

I'm implementing this for 3 angles: pi / 100, pi / 10 and pi / 6. I've come up with this so far:

x = [pi / 100, pi / 10, pi / 6];
y = sin(x);
e = (y - x) ./ y;

This works fine. Now, I want to unpack the result into 3 variables. I know I can do this:

RelativeError100 = e(1);
RelativeError10 = e(2);
RelativeError6 = e(end);

But coming from a python background, this seems a bit clunky to me. So I tried this:

[RelativeError100, RelativeError10, RelativeError6] = e

Which errors out, giving Too many output arguments.. So, I did a little research, and came across this which suggests

[RelativeError100, RelativeError10, RelativeError6] = e{:}

Which throws a Cell contents reference from a non-cell array object..

What am I missing, and how can I get this to work? Or is it just not possible with matlab?

Hunter Jiang
2#
Hunter Jiang Reply to 2018-02-13 05:27:18Z

What about:

x = [pi / 100, pi / 10, pi / 6];
y = sin(x);
e = num2cell((y - x) ./ y);
[RelativeError100, RelativeError10, RelativeError6] = e{:}

It's since {:} only works when e is a cell, but not a matrix.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.447067 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO