Thread Subject: Function output dimensions

Subject: Function output dimensions

From: André Brito

Date: 9 Sep, 2009 21:05:23

Message: 1 of 3

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.

(Maybe it has been answered before how it can be done, or that this is not the correct way to do it, or I'm missing something really obvious but I couldn't find a similar problem with the search terms I've used)

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,
Andr

Subject: Function output dimensions

From: jrenfree

Date: 9 Sep, 2009 21:33:07

Message: 2 of 3

On Sep 9, 2:05 pm, "Andre Brito" <andre_brit...@hotmail.com> wrote:
> 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.
>
> (Maybe it has been answered before how it can be done, or that this is not the correct way to do it, or I'm missing something really obvious but I couldn't find a similar problem with the search terms I've used)
>
> 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,
> Andr

you would have to do:

[ab(1) ab(2)] = ind2sub([3 3],5);

By specifying only one output for ind2sub, such as when you type:

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

it doesn't care that ab used to be a 2x1 vector. It will override
that and make it a 1x1 vector, since you're telling ind2sub to only
return one variable.

Subject: Function output dimensions

From: André Brito

Date: 9 Sep, 2009 21:56:04

Message: 3 of 3

> you would have to do:
>
> [ab(1) ab(2)] = ind2sub([3 3],5);
>
> By specifying only one output for ind2sub, such as when you type:
>
> ab = ind2sub([3 3],5);
>
> it doesn't care that ab used to be a 2x1 vector. It will override
> that and make it a 1x1 vector, since you're telling ind2sub to only
> return one variable.

Thanks!

I just realized I don't have a question in my original post!!!

I realize I can do what you suggest but 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(...)

Cheers, Andr

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
reference Sprinceana 9 Sep, 2009 17:32:43
ind2sub Sprinceana 9 Sep, 2009 17:32:37
rssFeed for this Thread

Contact us at files@mathworks.com