what is the best mode to populate this array in this loop annidate?
1 次查看(过去 30 天)
显示 更早的评论
i want to code it to creare text for legend plot! (legend)
it's better to use string and strcat or use arraycell ?
if the better is the second how can i do it?
or is it better to create the text outside the nested loop?
str1="a";
wA=1;
str2="b";
wB=1;
n=5;
b=2;
valA=rand(n,1,1) %it's only for example..i don't use it :)
valB=rand(b,1,1)
clear str
count=0;
strtot="";
for i=1:n
for k=1:b
count=count+1;
if wA==1
strtot=strcat(str1,"-",num2str(valA(i)))
end
if wB==1
strtot=strcat(strtot," ** ",str2,"-",num2str(valB(k)))
end
strArr(count,:)=strtot;
end
end
0 个评论
回答(1 个)
Dyuman Joshi
2023-9-11
It would be better to use string instead of cells as they take less memory and easier to work with -
n=5;
b=2;
valA=rand(n,1,1) %it's only for example..i don't use it :)
valB=rand(b,1,1)
[A,B] = meshgrid(valA,valB);
"a-" + string(A(:)) + " ** b-" + string(B(:))
3 个评论
Dyuman Joshi
2023-9-11
编辑:Dyuman Joshi
2023-9-11
I am well aware that your code is different and that you have used wA and wB in your code inside the for loop, but I was giving a general example.
You can always modify the code according to requirement.
Let's consider that the input are -
A = [2 3 5 7];
B = [1 4 6 8];
What should be the output if
1 - wA is zero and wB is one?
2 - wA is one and wB is zero?
3 - wA is one and wB is one?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!