Two easy questions (But I am confused)

3 views (last 30 days)
Hi!
  1. Say I have a matrix S that is of 100x100 size. Now, I want to create an array just by taking the diagonal values of S. So, the matrix will be a 1x100 double. How can I do that using a for loop?
  2. Say, I want to create a cell that will have strings like 'Row 1', 'Row 2', 'Row 3', ....., 'Row 100'. How can I do that?

Accepted Answer

Matt J
Matt J on 28 Jul 2022
No need for (explicit) loops:
S=rand(100);
Sdiag=diag(S);
cellstr("Row "+(1:100))
ans = 1×100 cell array
{'Row 1'} {'Row 2'} {'Row 3'} {'Row 4'} {'Row 5'} {'Row 6'} {'Row 7'} {'Row 8'} {'Row 9'} {'Row 10'} {'Row 11'} {'Row 12'} {'Row 13'} {'Row 14'} {'Row 15'} {'Row 16'} {'Row 17'} {'Row 18'} {'Row 19'} {'Row 20'} {'Row 21'} {'Row 22'} {'Row 23'} {'Row 24'} {'Row 25'} {'Row 26'} {'Row 27'} {'Row 28'} {'Row 29'} {'Row 30'} {'Row 31'} {'Row 32'} {'Row 33'} {'Row 34'} {'Row 35'} {'Row 36'} {'Row 37'} {'Row 38'} {'Row 39'} {'Row 40'} {'Row 41'} {'Row 42'} {'Row 43'} {'Row 44'} {'Row 45'} {'Row 46'} {'Row 47'} {'Row 48'} {'Row 49'} {'Row 50'} {'Row 51'} {'Row 52'} {'Row 53'} {'Row 54'} {'Row 55'} {'Row 56'} {'Row 57'} {'Row 58'} {'Row 59'} {'Row 60'} {'Row 61'} {'Row 62'} {'Row 63'} {'Row 64'} {'Row 65'} {'Row 66'} {'Row 67'} {'Row 68'} {'Row 69'} {'Row 70'} {'Row 71'} {'Row 72'} {'Row 73'} {'Row 74'} {'Row 75'} {'Row 76'} {'Row 77'} {'Row 78'} {'Row 79'} {'Row 80'} {'Row 81'} {'Row 82'} {'Row 83'} {'Row 84'} {'Row 85'} {'Row 86'} {'Row 87'} {'Row 88'} {'Row 89'} {'Row 90'} {'Row 91'} {'Row 92'} {'Row 93'} {'Row 94'} {'Row 95'} {'Row 96'} {'Row 97'} {'Row 98'} {'Row 99'} {'Row 100'}

More Answers (1)

Les Beckham
Les Beckham on 28 Jul 2022
Edited: Les Beckham on 28 Jul 2022
Using smaller examples so we can see the results:
S = magic(10) % sample matrix
S = 10×10
92 99 1 8 15 67 74 51 58 40 98 80 7 14 16 73 55 57 64 41 4 81 88 20 22 54 56 63 70 47 85 87 19 21 3 60 62 69 71 28 86 93 25 2 9 61 68 75 52 34 17 24 76 83 90 42 49 26 33 65 23 5 82 89 91 48 30 32 39 66 79 6 13 95 97 29 31 38 45 72 10 12 94 96 78 35 37 44 46 53 11 18 100 77 84 36 43 50 27 59
d = diag(S) % extract the diagonal elements
d = 10×1
92 80 88 21 9 42 30 38 46 59
% note that this function is undocumented but quite useful for creating cell arrays
sprintfc('Row%d', 1:10)
ans = 1×10 cell array
{'Row1'} {'Row2'} {'Row3'} {'Row4'} {'Row5'} {'Row6'} {'Row7'} {'Row8'} {'Row9'} {'Row10'}
  3 Comments
Steven Lord
Steven Lord on 28 Jul 2022
If you're trying to create an array of text to use as legend entries and you're using a release that supports string, you don't need to create a cellstr for this purpose.
legendStrings = strings(1, 5);
x = 0:360;
axis([0, 360, -1, 1]) % x ranges from 0 to 360, y ranges from -1 to +1
hold on
for k = 1:5
plot(x, sind(k*x))
legendStrings(k) = "sine of " + k + "*x";
end
legend(legendStrings)
Alternately you could set each line's DisplayName property when you create it. If you do that you don't have to maintain a separate list of legend strings. All you'd have to do at the end of your code is tell MATLAB to show the legend. I'm using cosine for this one instead of sine so you see I didn't just copy and paste.
figure
x = 0:360;
axis([0, 360, -1, 1]) % x ranges from 0 to 360, y ranges from -1 to +1
hold on
for k = 1:5
plot(x, cosd(k*x), 'DisplayName', "cosine of " + k + "*x");
end
legend show

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!