MATLAB Answers

Selecting only half a row of an array

15 views (last 30 days)
Jay Perks
Jay Perks on 21 Sep 2020
Commented: Jay Perks on 21 Sep 2020 at 5:15
Hey guys,
Im fairly new to MATLAB and I'm just having a problem working out how to multiply ony half a row of my array.
I was hoping someone could help me out.
Basic example
a = 1x5
1
2
3
4
5
6
How would I select 2, 3, 4 and multiply it by 3
and how would I select 4, 5, 6 and multiply it by 3

  0 Comments

Sign in to comment.

Accepted Answer

KSSV
KSSV on 21 Sep 2020
Read about MATLAB array indexing, instead of asking very simple basic questions.
If a is an narray of size 1x5.
a(1:2) % gives first two arrays
a(1) % gives first
a(end) % gives last element
a(2:4)
iwant = a(1:3)*3 ; % pick first three and multiply by 3

  3 Comments

Jay Perks
Jay Perks on 21 Sep 2020
Thanks for the answer KSSV,
I had a look over indexing and I think I now know where I have gone wrong but comes another question.
I have an array of 201x1 with values of forces for a truss over a domain of -90 to 90 degrees.
When calculating the values, the values from 101 to 201 are to be negative to which I was going to multiply by -1.
when I select the range to be -1, how do I keep the first 101 values?
example
BA = cosd(DOMAIND); % BA = 201x1
BA =BA(101:201,1).*-1; % BA = 101x1
Do I need to add this onto my BA array or is there a better way of getting my full 201x1 array with half the values negative?
KSSV
KSSV on 21 Sep 2020 at 4:54
BA = zeros(201,1) ;
BA = cosd(DOMAIND); % BA = 201x1
BA =BA(101:201,1).*-1; % BA = 101x1
Jay Perks
Jay Perks on 21 Sep 2020 at 5:15
Thanks KSSV!
In the end I used the below command to keep my 201x1 array without it changing to 101x1.
BA(101:201,1) = BA(101:201,1).*-1
Thankyou!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by