Simple subtraction from column/row - HELP

3 views (last 30 days)
Leighton
Leighton on 17 Nov 2014
Commented: Leighton on 18 Nov 2014
Hello, let me first introduce myself. I am a 32 (nearly 33) year old mature student in my final year of engineering study.
Secondly, I am a complete NOVICE when it comes to matlab but have chosen this module during my final year of study as I believe matlab will benefit me more in the future as opposed to CFD (for example) or stress analysis via a CAD package!
Anyway, I have an issue which I can't seem to iron out...
Brief description of what I am trying to do;
Am compiling a matrix [m,n] (in my current example, is [4, 7]) and need to subtract a constant from each column in a linear manner.
Using the 3x5 as an example below if subtracting 5 from 50, the desired result is;
50 45 40 35 30
50 0 0 0 0
50 45 40 35 30
Instead I'm getting;
50 45 45 45 45
50 0 0 0 0
50 45 45 45 45
My for loop takes the shape;
for i=[1:a-1]
C(1,i+1)=T-td;
C(b,i+1)=T-td;
end
Where T = 50 and td = 8.333 (not that I imagine they are relevant to you mega minds).
So what I need is to know where I am going wrong (assuming the T-td is at fault somehow) and best way to fix it please?
I will just add that this is part of an assignment I have on but by you providing me with assistance, you are not aiding a cheat. I am perfectly compliant within rules and regs for seeking knowledge from more experienced and reliable sources. Getting to see my lecturer can be tricky (lecture times/ school pick-ups etc) so just trying to save time by coming on here.
I have looked on several pages here and on 'help' at the campus but I'm as much use as a chocolate fireguard sometimes and now is one of them times... Can't find what I need (or it's just not making sense).
I am not totally sure what version MatLab is on the campus systems either (chocolate fireguard?), think they appear to vary from stacks that were connected to the network but no longer are and stacks that are connected to a network so may range from 2010-2014 (possibly).
I thank you in advance for any help you may provide. Also, would you offer a brief description to better my understanding (really struggle with for loops, just like I have with differentiation for years - enjoy basic matrices and integration though (strange boy!)).
  6 Comments
Leighton
Leighton on 17 Nov 2014
It's almost my desired output but I only need to subtract from first and last rows (leaving 0 in all other rows/columns as displayed).
I've looked at my previous LU decomposition, taylor series and gaussian codes but can't find anything of help!!
Leighton
Leighton on 18 Nov 2014
Edited: Leighton on 18 Nov 2014
Nevermind, I've sussed it.
For anyone interested, i found this sorted it;
for i=[1:a-1]
for j=i+1;
C(1,j)=C(1,i)-td;
C(end,j)=C(end,i)-td;
end
end
Just needs an if statement to deal with negligible but potential negative decimals.

Sign in to comment.

Answers (1)

MA
MA on 18 Nov 2014
clear all
close all
clc;
A=[50;50;50];
a=5;
for i=1:a-1
A(1,i+1)=A(1,i)-a;A(2,i+1)=0;A(3,i+1)=A(1,i)-a;
end
A

Community Treasure Hunt

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

Start Hunting!