MATLAB Answers

onrammp exercise 14.2 stellar motion part 2, task 6 movaway

119 views (last 30 days)
For task 6 I don't understand how matlab connects starname to a particular graph.
Also I'm not sure how this piece of code works...
"movaway = starnames(speed < 0) "
how does this line of code use a variable that is in a loop before this line of code?
Sorry in advance if this is an obvious question.
Task 1
[sHa,idx] = min(spectra);
lambdaHa = lambda(idx);
z = lambdaHa/656.28 - 1;
speed = z*299792.458
Tasks 2 - 4
for c = 1:7
s = spectra(:,c);
if speed(c) >= 0
loglog(lambda,s,"--")
else
loglog(lambda,s,"LineWidth",3)
end
hold on
end
hold off
Task 5
legend(starnames)
Task 6
movaway = starnames(speed < 0)

  0 Comments

Sign in to comment.

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 3 Jan 2020
Edited: KALYAN ACHARJYA on 3 Jan 2020
See the example, lets suppose starnames is variable having all types of numbers
starnames=[-2 3 45 7 2 0 -4 5 7 7 6 9];
speed=[2 3 -4 5 6 7 8 0 5 2 -30] ; % Just random example
## speed<0 is an conditional assignment, its return a logical array (with 0 & 1), when condition true, its 1, otherwise 0
>> speed < 0
ans =
1×11 logical array
0 0 1 0 0 0 0 0 0 0 1
## starnames(speed < 0) return an array, where the 1 presence in the above logical array
>> movaway = starnames(speed < 0)
movaway =
45 6
This is just an example to understand the issue (This approach is good way for efficient code, conditional & array indexing)

  1 Comment

Darnell Gawdin
Darnell Gawdin on 3 Jan 2020
Thank you for your time and expertise to answer this question. I see how it works now.

Sign in to comment.

More Answers (3)

Bongani Tavengwa
Bongani Tavengwa on 5 Jun 2020
Task
Add an if statement to the for loop body. If speed(c) is less than or equal to 0, create a loglog plot of s against lambda using a dashed line (--).
After the if statement, add the command hold on so that only one plot is created.
for c = 1:7
s = spectra(:,c);
if speed(c) >= 0
loglog(lambda,s,"--")
else
loglog(lambda,s,"LineWidth",3)
end
hold on
end
please help me where do i go wrong

  6 Comments

Show 3 older comments
Steven Lord
Steven Lord on 5 Jun 2020
Use the less than or equal to operator instead of the greater than or equal to operator.
Sergio Andres Cuellar Oliveros
I get an error, having all the code fine, then he asks me: Is there a graph with seven lines?, please help me
for c = 1:7
s = spectra(:,c);
if speed(c)<= 0
loglog(lambda,s,"--")
else
loglog(lambda,s,"LineWidth",3)
end
hold on
end
hold off

Sign in to comment.


Ahmed Mohamed Abo Bakr
Ahmed Mohamed Abo Bakr on 21 Oct 2020
First, you'll plot the blueshifted spectra using dashed lines.
TASKAdd an if statement to the for loop body. If speed(v) is less than or equal to 0, create a loglog plot of s against lambda using a dashed line (--).
After the if statement, add the command hold on so that only one plot is created.

  0 Comments

Sign in to comment.


Products

Community Treasure Hunt

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

Start Hunting!