Thread Subject: [ab(1) ab(2) ... ab(N)] ~= [ab(1:N)] ???

Subject: [ab(1) ab(2) ... ab(N)] ~= [ab(1:N)] ???

From: André Brito

Date: 10 Sep, 2009 08:38:03

Message: 1 of 5

Hi,
basically, what I'm trying to do is to get the output of ind2sub (or of any function, for that matter) to output a vector and assign it to an existing vector. I know I could assign each element of the output to each element of the existing vector. The problem is when the dimensions of the output are not fixed.

What I want to do is to be able to specify an output without MATLAB overwriting it and checking its dimensions. My idea is for this function to work regardless of the dimensions, such as replacing:
[ab(1) ab(2) ... ab(N)] = function(...)
with
[ab(1:N)] = function(...)

As an example, could someone just explain me what is the mechanism behind MATLAB that makes, for example, the function ind2sub output the following:

>> [a b] = ind2sub([3 3],5)
a = 2
b = 2

but if
>> ab = [0 0];
>> ab = ind2sub([3 3],5)
ab = 5

Meaning that it does not detect that the output vector has dimensions different than 1x1.

Cheers,
Andre

Subject: [ab(1) ab(2) ... ab(N)] ~= [ab(1:N)] ???

From: Nasser Abbasi

Date: 10 Sep, 2009 09:45:29

Message: 2 of 5


"Andre Brito" <andre_brito23@hotmail.com> wrote in message
news:h8adtb$r4u$1@fred.mathworks.com...

....

> As an example, could someone just explain me what is the mechanism behind
> MATLAB that makes, for example, the function ind2sub output the following:
>
>>> [a b] = ind2sub([3 3],5)
> a = 2
> b = 2
>
> but if
>>> ab = [0 0];

Here you declare ab to be a vector of size 2

>>> ab = ind2sub([3 3],5)
>>> ab = 5

But here ind2sub overwrite your variable 'ab' which is a vector, with the
outout of ind2sub. And since you prodvided one place for it to write its
ouuput, it returned one of the output parameters and wrote that into 'ab',
which is a scalar. You old vector 'ab' got expunged I am afraid.

If you wanted ind2sub to write into your vector 'ab', then do

EDU>> ab(1) = ind2sub([3 3],5)

ab =

     5 0

hth

--Nasser

>
> Meaning that it does not detect that the output vector has dimensions
> different than 1x1.
>



> Cheers,
> Andre

Subject: [ab(1) ab(2) ... ab(N)] ~= [ab(1:N)] ???

From: André Brito

Date: 10 Sep, 2009 09:52:01

Message: 3 of 5

> You old vector 'ab' got expunged I am afraid.

Thanks, but do you have an idea how to do it without getting expunged?

Andre

Subject: [ab(1) ab(2) ... ab(N)] ~= [ab(1:N)] ???

From: Bruno Luong

Date: 10 Sep, 2009 10:29:05

Message: 4 of 5

"Andre Brito" <andre_brito23@hotmail.com> wrote in message <h8ai80$jp2$1@fred.mathworks.com>...
> > You old vector 'ab' got expunged I am afraid.
>
> Thanks, but do you have an idea how to do it without getting expunged?
>

Try this:

abc = cell(size(ab));
[abc{:}] = ind2sub([3 3],2,2);
ab = [abc{:}]

Sorry but I don't have time right now to give a more proper explanation; but you should look at comma separated list, function calling, nargout.

You should also try to understand the way ind2sub contract the first input (size) with respect to the number of outputs.

Bruno

Subject: [ab(1) ab(2) ... ab(N)] ~= [ab(1:N)] ???

From: André Brito

Date: 10 Sep, 2009 12:47:01

Message: 5 of 5

> Try this:
>
> abc = cell(size(ab));
> [abc{:}] = ind2sub([3 3],2,2);
> ab = [abc{:}]
>
> Sorry but I don't have time right now to give a more proper explanation; but you should look at comma separated list, function calling, nargout.
>
> You should also try to understand the way ind2sub contract the first input (size) with respect to the number of outputs.
>
> Bruno

Thanks a lot!
I do understand the way ind2sub works.

The tip that you gave is to save the output as a cell instead of a vector!

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
varargout Andre Brito 10 Sep, 2009 08:49:03
nargout Andre Brito 10 Sep, 2009 08:49:03
function output Andre Brito 10 Sep, 2009 08:49:02
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com