Keeping the same order in arrays - Brain Teaser?

1 view (last 30 days)
**EDIT: I need the output to be a matrix because a much larger part of the code needs the output of this code to be a matrix for it's input****
Hello. I have a question related to matrix manipulation.
I need to keep the order the same in the matrix. Please see below for what I am trying to do.
lets say I start out with 3 fruits:
fruits = {'apple','orange','berry'};
and the amounts of each fruit:
amount = [3,5,2]
then the next day the amount changes:
amount = [2,4,3]
so now, my matrix will be:
3 5 2
2 4 3
but what if the next day I needed to add another fruit:
fruits = {'apple','orange','berry','banana'};
and the amounts are:
amount = [3,4,2,1]
how do I make my new matrix like this:
3 5 2 NaN
2 4 3 NaN
3 4 2 1
then on the next day, I was not given 1 of the original fruits:
fruits = {'apple','berry','banana'};
and the amount would be:
amount = [5,1,4]
then I need the matrix to be like the following:
3 5 2 NaN
2 4 3 NaN
3 4 2 1
5 NaN 1 4
How would I write the code for it to be able to handle all of these situations?
  3 Comments
Viral Solanki
Viral Solanki on 23 Jun 2015
Yes, I did think of many other ways of doing this, but it needs to be in this format because of other integrated code. The other code I'm working with needs this particular input
Viral Solanki
Viral Solanki on 23 Jun 2015
I also could do it with a bunch of ifs and loops, but I wanted to learn about how to use matrix manipulation techniques. Don't want to keep making new dirty code

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 23 Jun 2015
Edited: Stephen23 on 23 Jun 2015
Try using a non-scalar structure (note that looping backwards is useful and not a mistake)
fruits{4} = {'apple','berry','banana'};
amount{4} = [5,1,4];
fruits{3} = {'apple','orange','berry','banana'};
amount{3} = [3,4,2,1];
fruits{2} = {'apple','orange','berry'};
amount{2} = [2,4,3];
fruits{1} = {'apple','orange','berry'};
amount{1} = [3,5,2];
for mm = numel(fruits):-1:1 % backwards!
for nn = 1:numel(fruits{mm})
out(mm).(fruits{mm}{nn}) = amount{mm}(nn);
end
end
And we can view the data:
>> {out.apple}
ans =
[3] [2] [3] [5]
>> {out.banana}
ans =
[] [] [1] [4]
Note the missing data are indicated by empty arrays.
  4 Comments
Viral Solanki
Viral Solanki on 23 Jun 2015
I can not even explain how grateful I am to you!

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 23 Jun 2015
Try using a table.
t = array2table([3 5 2], 'VariableNames', {'apple','orange','berry'});
t(end+1, {'apple', 'orange', 'berry'}) = {2, 4, 3};
t(end+1, {'apple', 'orange', 'berry', 'banana'}) = {3, 4, 2, 1};
t(end+1, {'apple', 'berry', 'banana'}) = {5, 1, 4};
This will give you a warning on the last line, but if you intentionally did not assign to the orange variable you can suppress it. It will also fill in with 0's instead of NaN but you can use standardizeMissing to replace 0 with NaN. See the examples in the table documentation for more information on how to work with this data type.
  1 Comment
Viral Solanki
Viral Solanki on 23 Jun 2015
This would be awesome, but there are 2 issues.
1. I am using an older version of matlab (R2012B) and
2. The code that is already integrated into the system needs a matrix as an input.
But, thank you for this. I will remember this

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!