How to create a vector out of arrays

16 ビュー (過去 30 日間)
Erica
Erica 2014 年 7 月 15 日
コメント済み: José-Luis 2014 年 7 月 15 日
Given the Following arrays:
Header = ('A' 'B' 'C' 'D')
Data1 = ( 1 2 3 4)
Data2 = (5,6,7,8)
I want to create the following Vectors:
A = (1 ,5)
B = (2, 6)
C = (3, 7)
D = (4, 8)
My attempt was this: for I = 1:length(Header) NewVector = [Header(i), ' = [Data1(i) Data2(i)]']; eval(NewVector); end
My error is: Undefined function 'eval' for input arguments of type 'cell'.
Help?

採用された回答

José-Luis
José-Luis 2014 年 7 月 15 日
Header = {'A' 'B' 'C' 'D'};
Data1 = [1 2 3 4];
Data2 = [5,6,7,8];
cnt = 1;
for ii = Header
assignin('base',ii{:},[Data1(cnt) Data2(cnt)]);
cnt = cnt + 1;
end
This is a bad idea though, you'd be better off storing in a cell array instead of dynamically creating variables.
  2 件のコメント
Erica
Erica 2014 年 7 月 15 日
Yes! This is perfect! I understand why you think this is a bad idea, but I have a purpose behind my madness. Thanks!!
José-Luis
José-Luis 2014 年 7 月 15 日
Fair enough. Glad to help.

サインインしてコメントする。

その他の回答 (3 件)

Ben11
Ben11 2014 年 7 月 15 日
編集済み: Ben11 2014 年 7 月 15 日
Try this:
clear all
clc
Header = {'A' 'B' 'C' 'D'};
Data1 = [1 2 3 4];
Data2 = [5,6,7,8];
NewVector = cell(1,numel(Header));
for i = 1:numel(Header)
NewVector{i} = sprintf('%s = (%i,%i)',Header{i},Data1(i),Data2(i));
end
NewVector
NewVector =
Columns 1 through 3
'A = (1,5)' 'B = (2,6)' 'C = (3,7)'
Column 4
'D = (4,8)'
Now each cell of the NewVector cell array contains what you want.
I must say that your question was not that clear so if it's not what you are looking for please don't hesitate to ask for details/clarify your question. For instance do you want every element to literally contain a letter with an equal sign and then a pair of numbers, or simply the pair of numbers? If so then you would not have to use cell arrays.
  1 件のコメント
Erica
Erica 2014 年 7 月 15 日
Yeah - I had trouble wording what I was after ... Yes, I literally want a Vector Named A and to have it's contents be 1 and 5 so that I can access A(0) and get 1 or A(1) and get 5... Do you see what I mean??

サインインしてコメントする。


James Tursa
James Tursa 2014 年 7 月 15 日
編集済み: James Tursa 2014 年 7 月 15 日
I am guessing that Header is a cell array of strings, even though you have typed it in your post using parentheses ( ) instead of curly braces { }. If that is the case, try this instead using curly brace {i} instead of parentheses (i):
[Header{i}, ' = [Data1(i) Data2(i)]']
I.e., use the curly braces to extract the string from Header before combining with the rest of the string. That way you will get a string result instead of a cell result.
Advice: In the future, post your exact code so people can have a chance to correct your syntax. By using parentheses ( ) in your post instead of the exact code { } and [ ], you unintentionally mislead the readers.

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 7 月 15 日
This is not a good idea.
Data1 = [1 2 3 4]
Data2 =[5,6,7,8]
Just create one variable
A=[Data1' Data2']

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by