How can I create variables from a matrix (4 x m), so that every column is a variable ? In the code I have 2 elements (N=2), but I need to do it also with 6, 8 10 elements.
5 次查看(过去 30 天)
显示 更早的评论
N=2; % elements
L=1/N;
L=zeros(1,N)+L;
y=0:L:1;
Nf=NaN;
syms x NaN
for i=1:length(y)
if i-1==0
fa(i)=NaN;
elseif i-1==1
fa(i)=NaN;
else fa(i)=0;
end
if i-1==0
fb(i)=NaN;
else
fb(i)=(x-y(i-1))/L(i-1);
end
if i+1>N+1
fc(i)=NaN;
else
fc(i)=(y(i+1)-x)/L(i);
end
if i+1>=N+1
fd(i)=Nf;
else
fd(i)=0;
end
end
f=[fa; fb; fc; fd]
3 个评论
DGM
2021-10-2
编辑:DGM
2021-10-2
This is relevant.
Long story short: arrays are indexable programmatically. Variable names are not programmatically indexable without opening up a can of worms that you should want to avoid unless you actually like causing problems for yourself.
As dpb mentioned, there are options like structs or tables that might be more desirable for the way they allow you to describe and organize their contents.
Ravi Narasimhan
2021-10-2
编辑:Ravi Narasimhan
2021-10-2
To pile on: I use tables a lot but also Map Containers for associating keys with values when appropriate.
e.g.
A = magic(4)
keySet = [1,2,3,4]; % Can be almost anything including chars if desired
valueSet = {A(:,1), A(:,2), A(:,3), A(:,4)}; % Associate a value with each key
B = containers.Map(keySet, valueSet)
B(3)
B(22) = [1 0 1 0] % Add to it. The 22 is a key vs. an array element
keys(B)
values(B)
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!