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!