1

So I want to decompose array to multiple variables. For example, I have 'data' array of (136,9) size which is of double type. I want to decompose the values of data(1,:) to multiple variables something like below:

[frm_id,seq_id,xmin,ymin,w,h,temp1,temp2,temp3] = data(1,:);

In python it was straightforward, but above code gives following error in matlab:

Insufficient number of outputs from right hand side of equal sign to satisfy
assignment.

I can go with something like

frm_id = data(1,1);
seq_id = data(1,2);
%ect

But I do believe there must be matlab (more neat) way to do this operation.

Thanks!

3
  • 1
    I haven't seen anything like that in Matlab. Why is frm_id = data(1,1); seq_id = data(1,2); not OK? because it does not look neat? Commented May 1, 2017 at 1:45
  • Yep, I need to write data(j,index) 9 times :) . So I am looking for something more neat Commented May 1, 2017 at 2:15
  • 2
    If you want to assign each of the 9 columns to a new array, then you have to write 9 assignments. Maybe consider using another data structure so you only have to write it once? Commented May 1, 2017 at 2:17

2 Answers 2

4

You can use num2cell to convert the matrix to a cell array then copy contents of the cell to each variable:

C = num2cell(data,1);
[frm_id,seq_id,xmin,ymin,w,h,temp1,temp2,temp3] = C{:};
Sign up to request clarification or add additional context in comments.

4 Comments

Didn't know cell array can be used like this, which is brilliant. Just a thought through my mind. Is the underlying returned value of a function a cell type?
@Anthony Which function? The returned value of num2cell is a cell type:)
any function. I've realised my question is off-topic already.
@rahnema1 Brilliant, that's what I was looking for!
1

I can only suggest you to create a function like this:

function [frm_id,seq_id,xmin,ymin,w,h,temp1,temp2,temp3] = myfunction (data)

frm_id = data(:,1);
seq_id = data(:,2);
xmin = data(:,3);
ymin = data(:,4);
w = data(:,5);
h = data(:,6);
temp1 = data(:,7);
temp2 = data(:,8);
temp3 = data(:,9);

so in your main code

[frm_id,seq_id,xmin,ymin,w,h,temp1,temp2,temp3] = myfunction(data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.